Exceptions
Uninitialized Value
Goal: implement an enumeration to avoid the use of uninitialized values.
Steps:
Implement the
Options
package.
Declare the
Option
enumeration type.Declare the
Unitialized_Value
exception.Implement the
Image
function.
Requirements:
Enumeration
Option
contains:
the
Unitialized
value, andthe actual options:
Option_1
,
Option_2
,
Option_3
.Function
Image
returns a string for theOption
type.
In case the argument to
Image
isUnitialized
, the function must raise theUnitialized_Value
exception.
Remarks:
In this exercise, we employ exceptions as a mechanism to avoid the use of uninitialized values for a certain type.
Numerical Exception
Goal: handle numerical exceptions in a test procedure.
Steps:
Add exception handling to the
Check_Exception
procedure.
Requirements:
The test procedure
Num_Exception_Test
from theTests
package below must be used in the implementation ofCheck_Exception
.The
Check_Exception
procedure must be extended to handle exceptions as follows:
If the exception raised by
Num_Exception_Test
isConstraint_Error
, the procedure must display the message "Constraint_Error detected!" to the user.Otherwise, it must display the message associated with the exception.
Remarks:
You can use the
Exception_Message
function to retrieve the message associated with an exception.
Re-raising Exceptions
Goal: make use of exception re-raising in a test procedure.
Steps:
Declare new exception:
Another_Exception
.Add exception re-raise to the
Check_Exception
procedure.
Requirements:
Exception
Another_Exception
must be declared in theTests
package.Procedure
Check_Exception
must be extended to re-raise any exception. When an exception is detected, the procedure must:
display an user message (as implemented in the previous exercise), and then
Raise or re-raise exception depending on the exception that is being handled:
In case of
Constraint_Error
exception, re-raise the exception.In all other cases, raise
Another_Exception
.
Remarks:
In this exercise, you should extend the implementation of the
Check_Exception
procedure from the previous exercise.
Naturally, you can use the code for the
Check_Exception
procedure from the previous exercise as a starting point.