DADiSP Developer's Guide > Chapter 2: SPL > SPL Error Handlers

 

SPL Error Handlers

 

Optional user defined SPL error handlers are supported in the form:

 

func_error(errnum, errmes) 

 

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:

 

myfun(x)

{

    return(x*x);

}

 

Running: 

 

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:

 

myfun_error(errnum, errmes)

{

    message(sprintf("myfun error %d, %s", errnum, errmes));

 

    return(-1);

}

 

Running:

 

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:

 

myfun_error(errnum, errmes) 

{ 

    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.

 

Try-Catch

 

The try-catch clause performs localized exception handling to allow an SPL routine to handle an error and optionally continue processing.

 

trytest(str) 

{ 

    local len;

 

    try

    {

        len = strlen(str);

    }

    catch

    {

        len = strlen(caststring(str));

    }

 

    return(len);

}

 

 

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.