11.2 Exception Handlers
Syntax
exception_choice ::= exception_name |
others
{
choice (of an exception_handler)}
Within
this section, we sometimes abbreviate “
exception_choice”
to “
choice.”
Legality Rules
Ramification: Two
exception_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
=>”.
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.
An
exception_name
of a choice shall not denote an exception declared in a generic formal
package.
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
{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
Examples
Example 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(Exception_Message(E)); --
see 11.4.1
raise;
end;
Extensions to Ada 83
{
AI95-00114-01}
Different
exception_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
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.