Ada Reference Manual (Ada 2022)Legal Information
Contents   Index   References   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/5
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

Legality Rules

5.1/4
 An exception_name of an exception_choice shall denote an exception.
6
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. 
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. 

Static Semantics

9
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
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/5
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;

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe