Try...except Statements
Exceptions are handled within
try...except
statements. For example,
try
X := Y/Z;
except
on EZeroDivide do HandleZeroDivide;
end;
This statement attempts to divide
Y
by
Z
, but calls a routine named
HandleZeroDivide
if an EZeroDivide exception
is raised.
The syntax of a
try...except
statement is
try statementsexceptexceptionBlockend
where statements is a sequence of statements (delimited by semicolons) and exceptionBlock is either
another sequence of statements or
a sequence of exception handlers, optionally followed by
elsestatements
An exception handler has the form
onidentifier: typedostatement
where identifier: is optional (if included, identifier can be any valid identifier), type is a type used to represent
exceptions, and statement is any statement.
A
try...except
statement executes the statements in the initial statements list. If no exceptions are raised, the
exception block (exceptionBlock) is ignored and control passes to the next part of the program.
162
If an exception is raised during execution of the initial statements list, either by a raise statement in the statements
list or by a procedure or function called from the statements list, an attempt is made to 'handle' the exception:
If any of the handlers in the exception block matches the exception, control passes to the first such handler. An
exception handler 'matches' an exception just in case the type in the handler is the class of the exception or an
ancestor of that class.
If no such handler is found, control passes to the statement in the else clause, if there is one.
If the exception block is just a sequence of statements without any exception handlers, control passes to the
first statement in the list.
If none of the conditions above is satisfied, the search continues in the exception block of the next-most-recently
entered
try...except
statement that has not yet exited. If no appropriate handler, else clause, or statement list
is found there, the search propagates to the next-most-recently entered
try...except
statement, and so forth. If
the outermost
try...except
statement is reached and the exception is still not handled, the program terminates.
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;
163
on EOverflow do HandleOverflow;
on EMathError do HandleMathError;
else
HandleAllOthers;
end;
Here, the else clause handles any exception that isn't an EMathError.
An exception block that contains no exception handlers, but instead consists only of a list of statements, handles all
exceptions. For example,
try
...
except
HandleException;
end;
Here, the
HandleException
routine handles any exception that occurs as a result of executing the statements
between try and except.
Dostları ilə paylaş: |