Next topic | Ada Home Page | Index

Exceptions

Exception = unexpected situation

Solutions:

Checking for all possible errors makes code hard to follow

Ada's exception handling mechanism provides a clean way to handle exceptions

Example

The following program requires the user to enter an integer which must lie in the range 1 to 10:

    with TEXT_IO;
    procedure main is

	package int_io is new TEXT_IO.INTEGER_IO(INTEGER);
	use text_io, int_io;

	subtype numrange is integer range 1..10;
	num : numrange;

    begin -- main

	PUT ("Please enter an integer from 1 to 10 : ");
	GET (num); SKIP_LINE;

    end;

When the program is executed, there are a couple of ways that invalid data can be entered. This picture shows the two types of exceptions that result:


Exception handling

The above example shows that the "normal" behaviour of a program is to crash when an unexpected situation arises.

Ada provides a mechanism for handling exceptions so that programs can recover from these unexpected situations.

Here we explain:


Next topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 26 Feb 96