Ada Conformity Assessment Authority      Home Conformity Assessment   Test Suite ARGAda Standard
 
Annotated Ada Reference Manual (Ada 202x Draft 28)Legal Information
Contents   Index   References   Search   Previous   Next 

11.4.3 Example of Exception Handling

Examples

1
Exception handling may be used to separate the detection of an error from the response to that error: 
2/5
{AI95-00433-01} {AI12-0178-1} package File_System is
    type Data_Type is ...;

    type File_Handle is limited private;
3
    File_Not_Found : exception;
    procedure Open(F : in out File_Handle; Name : String);
        -- raises File_Not_Found if named file does not exist
4
    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
5/5
{AI12-0178-1}     ...
private
    ...

end File_System;
5.a/5
Reason: {AI12-0178-1} The first ... provides a place for Close to be declared, and the second ... provides a place for File_Handle to be completed.
5.1/5
{AI12-0178-1} package body File_System is
    ...
5.b/5
Reason: {AI12-0178-1} This ... provides a place for File_Exists and the body of Close to be declared. 
6/5
{AI95-00433-01} {AI12-0178-1} package body File_System is
    procedure Open(F : in out File_Handle; Name : String) is
    begin
        if File_Exists(Name) then
            ...
        else
            raise File_Not_Found with "File not found: " & Name & ".";
        end if;
    end Open;
7
    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;
8
    ...
9
end File_System;
10/5
{AI12-0178-1} with Ada.Text_IO;
with Ada.Exceptions;
with File_System; use File_System;
use Ada;
procedure Main is
    Verbosity_Desired : Boolean := ...;
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;
11
In the above example, the File_System package contains information about detecting certain exceptional situations, but it does not specify how to handle those situations. Procedure Main specifies how to handle them; other clients of File_System might have different handlers, even though the exceptional situations arise from the same basic causes. 

Wording Changes from Ada 83

11.a/3
{AI05-0299-1} The sections labeled “Exceptions Raised During ...” are subsumed by this subclause, and by parts of Clause 9.

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