11.2 Exception Handlers
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[The response to one or more exceptions is specified by an exception_handler
.]
Syntax
2handled_sequence_of_statements
::=
sequence_of_statements
[exception
exception_handler
{exception_handler
}]
3/5exception_handler
::=
when [choice_parameter_specification
:] exception_choice
{'|' exception_choice
} =>
sequence_of_statements
4choice_parameter_specification
::=
defining_identifier
5exception_choice
::=
exception_name
| others
exception_handler
”.Legality Rules
5.1/4An exception_name
of an exception_choice
shall denote an exception.
A choice with an exception_name
covers the named exception. A choice with others covers all exceptions not named by previous choices of the same handled_sequence_of_statements
. Two choices in different exception_handler
s of the same handled_sequence_of_statements
shall not cover the same exception.
exception_choice
s of the same exception_handler
may cover the same exception. For example, given two renaming declarations in separate packages for the same exception, one may nevertheless write, for example, “when Ada.Text_IO.Data_Error | My_Seq_IO.Data_Error =>”.A choice with others is allowed only for the last handler of a handled_sequence_of_statements
and as the only choice of that handler.
An exception_name
of a choice shall not denote an exception declared in a generic formal package.
Static Semantics
9A choice_parameter_specification
declares a choice parameter, which is a constant object of type Exception_Occurrence (see 11.4.1). During the handling of an exception occurrence, the choice parameter, if any, of the handler represents the exception occurrence that is being handled.
Dynamic Semantics
10The execution of a handled_sequence_of_statements
consists of the execution of the sequence_of_statements
. [The optional handlers are used to handle any exceptions that are propagated by the sequence_of_statements
.]
Examples
11Example of an exception handler:
begin
Open(File, In_File, "input.txt"); -- see A.8.2
exception
when E : Name_Error =>
Put("Cannot open input file : ");
Put_Line(Ada.Exceptions.Exception_Message (E)); -- see 11.4.1
raise;
end;
Extensions to Ada 83
exception_choice
s of the same exception_handler
may cover the same exception. This allows for “when Numeric_Error | Constraint_Error =>” even though Numeric_Error is a rename of Constraint_Error. This also allows one to “with” two different I/O packages, and then write, for example, “when Ada.Text_IO.Data_Error | My_Seq_IO.Data_Error =>” even though these might both be renames of the same exception. Wording Changes from Ada 83
handled_sequence_of_statements
is new. These are now used in all the places where handlers are allowed. This obviates the need to explain (in Clauses 5, 6, 7, and 9) what portions of the program are handled by the handlers. Note that there are more such cases in Ada 95.choice_parameter_specification
is new.