Panic in Ada
This Ada program demonstrates the use of exceptions, which are similar to panics in some ways. Here’s a breakdown of the translation:
We import necessary Ada packages for input/output and exception handling.
The
Raise_Exception
procedure simulates thepanic("a problem")
call in the original code.The
Create_File
procedure attempts to create a file, and raises an exception if it fails, similar to the file creation attempt in the original code.In the main procedure, we call
Raise_Exception
first, which will cause the program to terminate with an exception.We then have the
Create_File
call, which won’t be reached due to the previous exception, similar to the original code.The exception handler at the end catches any unhandled exceptions, prints the exception name and message, and sets the exit status to failure.
To run this program:
Running this program will cause it to raise an exception, print an error message, and exit with a non-zero status.
When the first exception in Main
is raised, the program exits without reaching the rest of the code. If you’d like to see the program try to create a temp file, comment out the Raise_Exception
call.
Note that unlike some languages which use return codes for error handling, in Ada it is idiomatic to use exceptions for handling many errors, similar to the example shown here.