The exception handling block starts with the except keyword and ends with the keyword end. These two keywords are actually part of the same statement as the try block. That is, both the try block and the exception handling block are considered part of a single try...except statement.
Inside the exception handling block, you include one or more exception handlers. An exception handler is a statement of the form
on <type of exception> do <statement>;
For example, the following exception handling block includes multiple exception handlers for different exceptions that can arise from an arithmetic computation:
try { calculation statements } except on EZeroDivide do Value := MAXINT; on EIntOverflow do Value := 0; on EIntUnderflow do Value := 0; end;
Much of the time, as in the previous example, the exception handler doesn't need any information about an exception other than its type, so the statements following on..do are specific only to the type of exception. In some cases, however, you might need some of the information contained in the exception instance.
To read specific information about an exception instance in an exception handler, you use a special variation of on..do that gives you access to the exception instance. The special form requires that you provide a temporary variable to hold the instance. For example:
on E: EIntegerRange do
ShowMessage(Format('Expected value between %d and %d', E.Min, E.Max));
The temporary variable (E in this example) is of the type specified after the colon (EIntegerRange in this example). You can use the as operator to typecast the exception into a more specific type if needed.
try { statements } except on ESomething do { specific exception-handling code }; else { default exception-handling code }; end;
Adding default exception handling to a block guarantees that the block handles every exception in some way, thereby overriding all handling from any containing block.
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|