Exceptions
Table4.adb: Exception handler, overflow, constraint
error and data error exceptions
- Table4.adb
( prettified )
- This version handles invalid input and also
overflow during the calculations
- This version also uses a procedure to factor out
common behavior from
table3c.adb
( prettified )
- Exceptions handled by this routine
- Invalid Integer causes Data_Error
- Overflow causes Constraint_Error
- Must be compiled with -gnato to catch overflow
- skip_line skips all input through end of line (needed
for interactive operation to skip newline)
- Define before use: declaration must precede call
Exception blocks
begin
...
exception
when ... =>
...
when ... =>
...
end;
Flow of control:
-
when a statement causes an exception, execution flows to the
corresponding named when clause, if one exists, and then to
the corresponding end.
- If no corresponding when clause
exists, the program terminates.
when others handles any other exceptions
(How is this done in Java?)
Begin-end block can be put anywhere
Any begin end block can have an exception handler
Note: any exception can be ignored (Is this true in
Java?)
Raising Exceptions and User Defined Exceptions
- This program demonstrates the following:
- User defined exceptions
- Raising exceptions
- Package Ada.Exceptions
- Exception Names
- Multiple names on one clause
- Exceptions in a procedure begin-end block
- Example program: exceptions3.adb
( prettified )