Quick start manual

Classes and objects
7-31
Exceptions
When an exception is handled, the stack is traced back to the procedure or function
containing the try...except statement where the handling occurs, and control is
transferred to the executed exception handler, else clause, or statement list. This
process discards all procedure and function calls that occurred after entering the
try...except statement where the exception is handled. The exception object is then
automatically destroyed through a call to its Destroy destructor and control is passed
to the statement following the try...except statement. (If a call to the Exit, Break, or
Continue standard procedure causes control to leave the exception handler, the
exception object is still automatically destroyed.)
In the example below, the first exception handler handles division-by-zero
exceptions, the second one handles overflow exceptions, and the final one handles all
other math exceptions. EMathError appears last in the exception block because it is
the ancestor of the other two exception classes; if it appeared first, the other two
handlers would never be invoked.
try
ƒ
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
end;
An exception handler can specify an identifier before the name of the exception class.
This declares the identifier to represent the exception object during execution of the
statement that follows on...do. The scope of the identifier is limited to that statement.
For example,
try
ƒ
except
on E: Exception do ErrorDialog(E.Message, E.HelpContext);
end;
If the exception block specifies an else clause, the else clause handles any exceptions
that aren’t handled by the block’s exception handlers. For example,
try
ƒ
except
on EZeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
else
HandleAllOthers;
end;
Here, the else clause handles any exception that isn’t an EMathError.