At various times in the preceding chapters we have said that if something goes wrong when the program is executed, then an exception, often Constraint_Error, will be raised. In this chapter we describe the exception mechanism and show how remedial action can be taken when an exception occurs. We also show how we may define and use our own exceptions. Exceptions concerned with interacting tasks are dealt with in Chapter 20.
Handling exceptions
We have seen that if we break various language rules then an exception may be raised when we execute the program. There are four predefined exceptions (declared in the package Standard) of which we have met three so far
Constraint_Error This generally corresponds to something going out of range; this includes when something goes wrong with arithmetic such as an attempt to divide by zero.
Program_Error This will occur if we attempt to violate the control structure in some way such as running into the end of a function, breaking the accessibility rules or calling a subprogram whose body has not yet been elaborated – see Sections 10.1, 11.7 and 12.1.
Storage_Error This will occur if we run out of storage space, as for example if we called the recursive function Factorial with a large parameter – see Section 10.1.
The other predefined exception is Tasking_Error. This is concerned with tasking and so is dealt with in Chapter 20.
Note that for historical reasons the exception Constraint_Error is also renamed as Numeric_Error in the package Standard. Moreover, Numeric_Error is considered obsolescent and so should be avoided.
If we anticipate that an exception may occur in a part of a program then we can write an exception handler to deal with it. For example, suppose we write
begin
… -- sequence of statements
exception
when Constraint_Error =>
… -- do something
end;
If Constraint_Error is raised while we are executing the sequence of statements between begin and exception then the flow of control is interrupted and immediately transferred to the sequence of statements following the =>. The clause starting when is known as an exception handler.
As an illustration we could compute Tomorrow from Today by writing
begin
Tomorrow := Day'Succ(Today);
exception
when Constraint_Error =>
Tomorrow := Day'First;
end;
If Today is Day'Last (that is, Sun) then the attempt to evaluate Day'Succ(Today) causes the exception Constraint_Error to be raised.
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.