Recover in Ada
Ada provides a mechanism to handle exceptions, which are similar to panics in other languages. The raise
statement is used to raise an exception, and the exception
block is used to catch and handle exceptions.
In Ada, exceptions are used for error handling and recovery. The raise
statement is used to raise an exception, which is similar to panic in other languages. The exception
block is used to catch and handle exceptions, which is analogous to recover.
In this example, we define a procedure May_Raise_Exception
that raises a Constraint_Error
exception with a custom message. In the main procedure, we call May_Raise_Exception
within a block that has an exception handler.
If an exception is raised, the execution immediately jumps to the exception handler. The handler catches all exceptions (when others =>
), retrieves the exception message using Ada.Exceptions.Exception_Message
, and prints it.
To run this Ada program:
This demonstrates how Ada can recover from exceptions, allowing a program to handle errors gracefully and continue execution instead of terminating abruptly.