11.2. Exception Handlers


The response to one or more exceptions is specified by an exception handler.

    exception_handler ::=
       when exception_choice {| exception_choice} =>
    exception_choice ::= exception_name | others 

An exception handler occurs in a construct that is either a block statement or the body of a subprogram, package, task unit, or generic unit. Such a construct will be called a frame in this chapter. In each case the syntax of a frame that has exception handlers includes the following part:


The exceptions denoted by the exception names given as exception choices of a frame must all be distinct. The exception choice others is only allowed for the last exception handler of a frame and as its only exception choice; it stands for all exceptions not listed in previous handlers of the frame, including exceptions whose names are not visible at the place of the exception handler.

The exception handlers of a frame handle exceptions that are raised by the execution of the sequence of statements of the frame. The exceptions handled by a given exception handler are those named by the corresponding exception choices.


       --  sequence of statements
          PUT(" MATRIX IS SINGULAR ");
       when others =>
          PUT(" FATAL ERROR ");
          raise ERROR;


The same kinds of statement are allowed in the sequence of statements of each exception handler as are allowed in the sequence of statements of the frame. For example, a return statement is allowed in a handler within a function body.

References: block statement, declarative part, exception, exception handling, function body, generic body, generic unit, name, package body, raise statement, return statement, sequence of statements, statement, subprogram body, task body, task unit, and 9.1, visibility.