RAD Studio |  |

Exception-handling Statements
See Also
Collapse All

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

Copy Code

    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:

Copy Code

    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:

Copy Code

    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.

Warning: Never destroy the temporary exception object. Handling an exception automatically destroys the exception object. If you destroy the object yourself, the application attempts to destroy the object again, generating an access violation.
You can provide a single default exception handler to handle any exceptions for which you haven't provided specific handlers. To do that, add an else part to the exception-handling block:
Copy Code

    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.

Warning: It is not advisable to use this all-encompassing default exception handler. The else clause handles all exceptions, including those you know nothing about. In general, your code should handle only exceptions you actually know how to handle. If you want to handle cleanup and leave the exception handling to code that has more information about the exception and how to handle it, then you can do so using a finally block. For details about finally blocks, see Writing Finally Blocks.
See Also

Raising an Exception

Writing a Finally Block

Handling Classes of Exceptions

Scope of Exception Handlers

Reraising Exceptions

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!