Exception handling
Exception handling statements consist of: throw, try, catch, and finally. The concept of exception handling includes dealing with unusual results in a function and with errors and recovery from them. Exception handling that uses the try related statements is most useful with complex error handling and recovery. Testing for simple errors and unwanted results is usually handled most easily with familiar if or switch statements. In this section, the discussion and examples deal with simple situations, since explanation and illustration are the goals. The exception handling statements might seem clumsy or bulky here, but do not lose sight of the fact that they are very powerful and elegant in real world programming where error recovery can be very complex and require much code when using traditional statements.
Another advantage of using try related exception handling is that much of the error trapping code may be in a function rather than in the all the places that call a function.
Before getting to specifics, here is some generalized phrasing that might help working with exception handling statements. A function may have code in it to detect unusual results and to throw an exception. The function is called from inside a try statement block which will try to run the function successfully. If there is a problem in the function, the exception thrown is caught and handled in a catch statement block. If all exceptions have been handled when execution reaches the finally statement block, the final code is executed.
Remember these execution guides:
•
When a throw statement executes, the rest of the code in a function is ignored, and the function does not return a value.
•
A program continues in the next catch statement block after the try statement block in which an exception occurred., and any value thrown is caught in a parameter in the catch statement.
•
A program executes a finally statement block if all thrown exceptions have been caught and handled.
catch will receive an error object that can be printed directly as a string, and which will contain these properties
•
name - Name of the exception class, e.g. "ConversionError"
•
message - text of error, e.g. "1607: Variable "b" is undefined."
•
fileName - Name of the source file where error occurred, if available, e.g. "c:\foo\myscript.jsa"
•
lineNum - Line number if file where error occurred, if available, e.g. "173"
•
functionName - Name of executing function where error occurred, if available, e.g. "foobar"
The following simple script illustrates all exception handling statements. The main() function has try, catch, and finally statement blocks. The try block calls SquareEven(), which throws an exception if an odd number is passed to it. If an even number is passed to the function, then the number is squared and returned. If an odd number is passed, it is fixed, and an exception is thrown. When the throw statement executes, it passes an object, as an argument, with information for the catch statement to use.
For example, the script below, as shown, displays:
16
We caught odd and squared even.
If you change rtn = SquareEven(4) to rtn = SquareEven(3), the display is:
Fixed odd number to next higher even. 16
We caught odd and squared even.
function main(argc, argv)
{
var rtn;
try
{
rtn = SquareEven(4);
// No display here if number is odd
Screen.writeln(rtn);
}
catch (err)
{
// Catch the exception info
// that was thrown by the function.
// In this case, the info was returned
// in an object.
Screen.writeln(err);
Screen.write("Error occurred at line " + err.lineNum );
if ( err.fileName )
Screen.write(" of file " + err.fileName );
if ( err.functionName )
Screen.writeln(" in function " + err.functionName );
Screen.writeln("");
}
finally
{
// Finally, display this line after normal processing
// or exceptions have been caught.
Screen.writeln("We caught odd and squared even.");
}
Screen.write("Paused..."); Clib.getch();
} //main
// Check for odd integers
// If odd, make even, simplistic by adding 1
// Square even number
function SquareEven(num)
{
// Catch an odd number and fix it.
// "throw an exception" to be caught by caller
if ((num % 2) != 0)
{
num += 1;
throw {msg:"Fixed odd number to next higher even. ",
rtn:num * num};
// We throw an object here. We could have thrown
// a primitive, such as:
// throw("Caught and odd");
// We would have to alter the catch statement
// to expect whatever data type is used.
}
// Normal return for an even number.
return num * num;
} //SquareEven
This example script does not actually handle errors. Its purpose is to illustrate how exception handling statements work. For purposes of this illustration, assume that an odd number being passed to SquareEven() is an error or extraordinary event.