Singular : exception; Error : exception; Overflow, Underflow : exception;
handled_sequence_of_statements ::= sequence_of_statements [exception exception_handler {exception_handler}]
exception_handler ::= when [choice_parameter_specification:] exception_choice {| exception_choice} => sequence_of_statements
choice_parameter_specification ::= defining_identifier
exception_choice ::= exception_name | othersLegality Rules
begin Open(File, In_File, "input.txt"); -- see section File Management exception when E : Name_Error => Put("Cannot open input file : "); Put_Line(Exception_Message(E)); -- see section The Package Exceptions raise; end;
raise_statement ::= raise [exception_name];Legality Rules
raise Ada.IO_Exceptions.Name_Error; -- see section Exceptions in Input-Output
raise; -- re-raise the current exception
Static Semantics
package Ada.Exceptions is type Exception_Id is private; Null_Id : constant Exception_Id; function Exception_Name(Id : Exception_Id) return String;
type Exception_Occurrence is limited private; type Exception_Occurrence_Access is access all Exception_Occurrence; Null_Occurrence : constant Exception_Occurrence;
procedure Raise_Exception (E : in Exception_Id; Message : in String := ""); function Exception_Message(X : Exception_Occurrence) return String; procedure Reraise_Occurrence(X : in Exception_Occurrence);
function Exception_Identity(X : Exception_Occurrence) return Exception_Id; function Exception_Name(X : Exception_Occurrence) return String; -- Same as Exception_Name(Exception_Identity(X)). function Exception_Information(X : Exception_Occurrence) return String;
procedure Save_Occurrence(Target : out Exception_Occurrence; Source : in Exception_Occurrence); function Save_Occurrence(Source : Exception_Occurrence) return Exception_Occurrence_Access; private ... -- not specified by the language end Ada.Exceptions;
E'Identity returns the unique identity of the exception. The type of this attribute is Exception_Id.
Examples
with Ada.Exceptions; use Ada; package File_System is type File_Handle is limited private;
File_Not_Found : exception; procedure Open(F : in out File_Handle; Name : String); -- raises File_Not_Found if named file does not exist
End_Of_File : exception; procedure Read(F : in out File_Handle; Data : out Data_Type); -- raises End_Of_File if the file is not open
... end File_System;
package body File_System is procedure Open(F : in out File_Handle; Name : String) is begin if File_Exists(Name) then ... else Exceptions.Raise_Exception (File_Not_Found'Identity, "File not found: " & Name & "."); end if; end Open;
procedure Read(F : in out File_Handle; Data : out Data_Type) is begin if F.Current_Position <= F.Last_Position then ... else raise End_Of_File; end if; end Read;
...
end File_System;
with Ada.Text_IO; with Ada.Exceptions; with File_System; use File_System; use Ada; procedure Main is begin ... -- call operations in File_System exception when End_Of_File => Close(Some_File); when Not_Found_Error : File_Not_Found => Text_IO.Put_Line (Exceptions.Exception_Message(Not_Found_Error)); when The_Error : others => Text_IO.Put_Line("Unknown error:"); if Verbosity_Desired then Text_IO.Put_Line(Exceptions.Exception_Information (The_Error)); else Text_IO.Put_Line(Exceptions.Exception_Name (The_Error)); Text_IO.Put_Line(Exceptions.Exception_Message (The_Error)); end if; raise; end Main;
pragma Suppress(identifier [, [On =>] name]);
Legality Rules
When evaluating a dereference (explicit or implicit), check that the value of the name is not null. When passing an actual parameter to a formal access parameter, check that the value of the actual parameter is not null.
Check that the discriminants of a composite value have the values imposed by a discriminant constraint. Also, when accessing a record component, check that it exists for the current discriminant values.
Check that the second operand is not zero for the operations /, rem and mod.
Check that the bounds of an array value are equal to the corresponding bounds of an index constraint. Also, when accessing a component of an array object, check for each dimension that the given index value belongs to the range defined by the bounds of the array object. Also, when accessing a slice of an array object, check that the given discrete range is compatible with the range defined by the bounds of the array object.
Check that two arrays have matching components, in the case of array subtype conversions, and logical operators for arrays of boolean components.
Check that a scalar value is within the base range of its type, in cases where the implementation chooses to raise an exception instead of returning the correct mathematical result.
Check that a scalar value satisfies a range constraint. Also, for the elaboration of a subtype_indication, check that the constraint (if present) is compatible with the subtype denoted by the subtype_mark. Also, for an aggregate, check that an index or discriminant value belongs to the corresponding subtype. Also, check that when the result of an operation yields an array, the value of each component belongs to the component subtype.
Check that operand tags in a dispatching call are all equal. Check for the correct tag on tagged type conversions, for an assignment_statement, and when returning a tagged limited object from a function.
When a subprogram or protected entry is called, a task activation is accomplished, or a generic instantiation is elaborated, check that the body of the corresponding unit has already been elaborated.
Check the accessibility level of an entity or view.
Check that evaluation of an allocator does not require more space than is available for a storage pool. Check that the space available for a task or subprogram has not been exceeded.
Represents the union of all checks; suppressing All_Checks suppresses all checks.
Erroneous Execution
pragma Suppress(Range_Check); pragma Suppress(Index_Check, On => Table);
Go to the first, previous, next, last section, table of contents.