Exception handling routines intercept runtime exceptions and execute a block
of code which handles them. If there is no exception handler, Sheerpower
returns an exception message specifying what the exception was and where it
occurred. Sheerpower stops program execution or tries the offending statement
again. There is a lot to it. Details are
here.
There are two types of exception handlers: ATTACHED handlers and DETACHED
handlers. Attached handlers are the most commonly used handlers. They are
similar to the
try/catch blocks that other languages provide.
For an ATTACHED handler, a WHEN EXCEPTION IN statement is used. WHEN
EXCEPTION IN expects the USE block right after the block of code it
protects.
For a DETACHED handler, the statement WHEN EXCEPTION USE is used. When an
exception occurs in the protected block, WHEN EXCEPTION USE calls a handler
routine located in some other part of the program. The same handler routine
can be used by any number of WHEN EXCEPTION USE statements and can be placed
anywhere in a program.
The following functions are related to exception handling:
- _ERROR
- EXTYPE -- the exception type number
- EXLABEL$ -- the location in the code when the exception occurred
- EXTEXT$ -- a user friendly description of the exception
- SYSTEXT$ -- the underlying operating system's description of the exception (if any)
And the following statements are related to exception handling:
- CONTINUE -- continue execution with the statement that follows the statement that caused the exception
- RETRY -- retry the statement that caused the exception
- CAUSE -- given an exception type number, cause an exception