Contents Index Search Previous Next
11.2 Exception Handlers
1
[The response to one or more exceptions is specified
by an exception_handler.]
Syntax
2
handled_sequence_of_statements
::=
sequence_of_statements
[
exception
exception_handler
{
exception_handler}]
3
exception_handler
::=
when [
choice_parameter_specification:]
exception_choice {|
exception_choice} =>
sequence_of_statements
4
choice_parameter_specification
::= defining_identifier
5
exception_choice
::= exception_name |
others
5.a
To be honest: {handler}
``Handler'' is an abbreviation for ``exception_handler.''
5.b
{choice (of an exception_handler)}
Within this section, we sometimes abbreviate ``exception_choice''
to ``choice.''
Legality Rules
6
{cover (of a choice and 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_handlers
of the same
handled_sequence_of_statements
shall not cover the same exception.
6.a
Ramification: Two choices
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
=>''.
6.b
An others choice even
covers exceptions that are not visible at the place of the handler. Since
exception raising is a dynamic activity, it is entirely possible for
an others handler to handle an exception that it could not have
named.
7
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.
8
An exception_name
of a choice shall not denote an exception declared in a generic formal
package.
8.a
Reason: This is because
the compiler doesn't know the identity of such an exception, and thus
can't enforce the coverage rules.
Static Semantics
9
{choice parameter}
A
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
10
{execution (handled_sequence_of_statements)
[partial]} The 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
11
Example of an
exception handler:
12
begin
Open(File, In_File, "input.txt"); -- see A.8.2
exception
when E : Name_Error =>
Put("Cannot open input file : ");
Put_Line(Exception_Message(E)); -- see 11.4.1
raise;
end;
Extensions to Ada 83
12.a
{extensions to Ada 83}
The syntax rule for exception_handler
is modified to allow a choice_parameter_specification.
12.b
Different choices
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
12.c
The syntax rule for 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 Sections 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.
12.d
The syntax rule for choice_parameter_specification
is new.
Contents Index Search Previous Next Legal