Forum Discussion
Philip_Baird
12 years agoCommunity Expert
Hi Scott, before you ditch JScript, you may want to consider the following pattern I use to handle Exceptions between Script Units that allows the usage of try catch blocks without to much extra overhead.
function Catcher() {
var result;
try {
result = Thrower();
// If result was assigned and it has an Exception property, an Exception was
// returned so throw it to be caught in the catch block
if( result && result.Exception ) { throw result.Exception };
// If we get here, no Exception was thrown
Log.Message( result );
} catch( exObj ) {
Log.Message( exObj.message );
}
}
function Thrower() {
try {
// Will throw a Object doesn't support this property or method Exception
this.notLikelyToExist();
// Because of JScripts loose typing, functions can return different types which
// allows this pattern to return success values as well as Exceptions
return 123;
} catch( exObj ) {
// Wrap the Exception in an Object Literal with an "Exception" (or similar)
// property so it can be checked for in calling code
return { "Exception": exObj };
}
}
This can be expanded upon (as I have done) to utilise proper Exception classes instead of Object Literals for full "instanceof" Type checking.
Anyway, just a thought, it may be of some use.
function Catcher() {
var result;
try {
result = Thrower();
// If result was assigned and it has an Exception property, an Exception was
// returned so throw it to be caught in the catch block
if( result && result.Exception ) { throw result.Exception };
// If we get here, no Exception was thrown
Log.Message( result );
} catch( exObj ) {
Log.Message( exObj.message );
}
}
function Thrower() {
try {
// Will throw a Object doesn't support this property or method Exception
this.notLikelyToExist();
// Because of JScripts loose typing, functions can return different types which
// allows this pattern to return success values as well as Exceptions
return 123;
} catch( exObj ) {
// Wrap the Exception in an Object Literal with an "Exception" (or similar)
// property so it can be checked for in calling code
return { "Exception": exObj };
}
}
This can be expanded upon (as I have done) to utilise proper Exception classes instead of Object Literals for full "instanceof" Type checking.
Anyway, just a thought, it may be of some use.
Related Content
- 14 years ago
- 13 years ago
- 6 years ago
- 10 years ago
Recent Discussions
- 5 days ago
- 5 days ago