Optional user defined SPL error handlers are supported in the form:
where func is the original SPL function name, errnum is the system supplied error number and errmes is the original system error message. For example:
b = myfun();
terminates execution and displays an error message because the required input value was not supplied. Since execution was discontinued, variable b is not assigned. However, by specifying an error handler:
message(sprintf("myfun error %d, %s", errnum, errmes));
b = myfun();
pops up the message:
myfun error -102, Line 3 x: Uninitialized Variable
but variable b is now assigned the value -1, the result of the error handler. Of course, the error handler is not required to display or process the error number or message.
An error handler can also terminate execution simply by calling the error function. For example:
error(sprintf("myfun error %d, %s", errnum, errmes), 1);
is identical to the default behavior except in this case the error message is displayed in a pop-up box.
The errnum and errmes input arguments are optional for an error handler.
For convenience, the error handler is usually placed in the same file as the original SPL routine.
The try-catch clause performs localized exception handling to allow an SPL routine to handle an error and optionally continue processing.
len = strlen(str);
len = strlen(caststring(str));
a = trytest("Ten");
b = trytest(10);
a == 3
b == 2
Try-catch clauses may be nested.
Try-catch blocks are particularly useful when processing external routines or file manipulation functions that return errors and it is desired to continue the execution of the calling SPL routine.