next up previous contents index
Next: The try...except statement Up: The Pascal language Previous: Scope

Exceptions

 

As of version 0.99.7, Free Pascal supports exceptions. Exceptions provide a convenient way to program error and error-recovery mechanisms, and are closely related to classes.

Exception support is based on 3 constructs:

Raise
statements. To raise an exeption. This is usually done to signal an error condition.
Try ... Except
blocks. These block serve to catch exceptions raised within the scope of the block, and to provide exception-recovery code.
Try ... Finally
blocks. These block serve to force code to be executed irrespective of an exception occurrence or not. They generally serve to clean up memory or close files in case an exception occurs. code.

The raise statement

The raise statement is as follows:


Raise statement


This statement will raise an exception. If it is specified, the exception instance must be an initialized instance of a class, which is the raise type. The address exception is optional. If itis not specified, the compiler will provide the address by itself.

If the exception instance is omitted, then the current exception is re-raised. This construct can only be used in an exception handling block (see further).

Remark that control never returns after an exception block. The control is transferred to the first try...finally or try...except statement that is encountered when unwinding the stack. If no such statement is found, the Free Pascal Run-Time Library will generate a run-time error 217 (see also section (gif)).

As an example: The following division checks whether the denominator is zero, and if so, raises an exception of type EDivException



The class Exception is defined in the Sysutils unit of the rtl. (section (gif))




next up previous contents index
Next: The try...except statement Up: The Pascal language Previous: Scope

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998