In Ada, it’s idiomatic to communicate errors via exceptions, which is different from the approach used in some other languages. Ada’s exception handling mechanism allows for robust error management and recovery.
In this Ada program:
We define custom exception types No_More_Tea and Cant_Boil_Water.
The F function demonstrates raising a predefined exception (Constraint_Error) with a custom message.
The Make_Tea procedure shows how to raise custom exceptions.
In the main procedure, we use exception handlers to catch and handle different types of exceptions.
We use Ada.Exceptions.Exception_Message to get the message associated with an exception.
The others handler catches any unhandled exceptions, demonstrating how to get detailed exception information.
Ada’s exception handling mechanism allows for separation of normal code flow and error handling, making the code easier to read and maintain. Unlike some languages that use return values for error handling, Ada uses exceptions, which can be caught and handled at any level of the call stack.
To run this program, save it as error_handling.adb and compile it with an Ada compiler:
This will compile the Ada program and then run it, displaying the output which demonstrates the various error handling scenarios.