Ada Exceptions – Chapter 11 in LRM

 

Ada defines four standard exceptions (those defined in the package Standard)

l                                  Constraint_Error – whenever a value goes outside the range allowed by its type

l                                  Storage_Error

l                                  Tasking_Error

l                                  Program _Error

 

 

Then there are exceptions defined in other packages such as Ada.Text_IO;

l                                  Data_Error  - when sequence of characters doesn’t form a  legal Integer literal

l                                  Status_Error  - when file is already open

l                                  Use_Error – if you can’t open or create the file

l                                  Name_Error – if name is not legal

 

 

Three types of files –

l                                  In_File – for input – must exist before being opened

l                                  Out_File – will be created if doesn’t exist – will be scrapped if does exist

l                                  Append_File -  appends to the end of the file

 

 

What can you do with exceptions?

l                                  Declare them

l                                  Raise them

l                                  Handle them

 

 

Exception declaration  example - 

            Something_wrong : exception

 

Exception raise example –

            Raise Something_wrong;

 

Exception handler example

            Exception

            When Something_wrong =>  ada.text_io.put_line (“ error occurred – something is wrong”);

            End;

 

Exception handler example – embedded in begin..end block

      begin

            Exception

            When Something_wrong =>  ada.text_io.put_line (“ error occurred – something is wrong”);

            End;

      End;

 

 

How do exceptions behave?

 When an exception occurrence is raised, normal program execution is abandoned and control is transferred to an applicable exception_handler, if any. To handle an exception occurrence is to respond to the exceptional event. To propagate an exception occurrence is to raise it again in another context; that is, to fail to respond to the exceptional event in the present context.