This example shows the usage of
WHENEVER ... RAISE to
propagate a potential exception to the caller. First the
program defines the
foo function as exception handler
with
WHENEVER ANY ERROR CALL foo, then it calls the
do_exception function, which instructs the
runtime system to propagate a potential error to the caller.
As result, the division by zero in line #13 will be caught
by the error handler defined in the
MAIN block and
call the
foo function:
MAIN
DEFINE i INTEGER
WHENEVER ANY ERROR CALL foo
DISPLAY "Next function call will generate an exception"
DISPLAY do_exception(100, 0)
WHENEVER ANY ERROR STOP -- reset default handler for rest of program
...
END MAIN
FUNCTION do_exception(a, b)
DEFINE a, b INTEGER
WHENEVER ANY ERROR RAISE
RETURN a / b
END FUNCTION
FUNCTION foo()
DISPLAY "Exception caught, status: ", STATUS
END FUNCTION
Program output:
Next function call will generate an exception
Exception caught, status: -1202