Panic in Standard ML
In Standard ML, we use exceptions to handle unexpected errors, which is similar to the concept of panic in other languages. This program demonstrates how to raise and handle exceptions.
The main
function immediately raises an exception with the message “a problem”. This is equivalent to the panic in the original example.
The commented-out section shows how you might use exceptions to handle errors when working with files. In this case, if opening the file fails, an exception is raised with the error message.
To run this program, you would typically save it in a file with a .sml
extension and then use an SML interpreter or compiler. For example, if you’re using Standard ML of New Jersey (SML/NJ):
This output shows that an exception was raised with the message “a problem”, along with the file and line number where it occurred.
Note that unlike some languages which use exceptions for handling of many errors, in Standard ML it’s idiomatic to use option types or result types for expected error cases, reserving exceptions for truly exceptional situations.