[m-dev.] checked exceptions.

Paul Bone paul at bone.id.au
Wed Feb 13 10:32:24 AEDT 2013

How do people feel about checked exceptions?

The example I'm most familiar with is Java's implementation.  Some
exceptions are unchecked, these include out of memory, divide by zero etc.
Other exceptions are checked, these include file not found, permissions
errors, disk full and almost all user defined exceptions.

If your method might throw a checked exception then it must declare the
possibility of the exception as part of it's signature.  Any method calling
this method is forced either to catch and handle the exception, or to
declare that it may be thrown.

This could be useful in Mercury to make it even easier to write reliable
software.  We can already force the caller of a predicate to check for
errors by decomposing a type that our predicate returns.  Adding checked
exceptions adds this requirement for calls that may return an exception.
This should be useful when errors are rare enough to be written as
exceptions rather than represented in types, but not so rare as the
unexpected exception of the require module in the standard library (which
should probably be an unchecked exception).  It should also help where
wrapping the result of a computation can be difficult as a predicate may
return multiple values.

I propose we add this feature, its implementation should be straight forward
(except for syntax discussions).  If a majority aren't willing to support the
idea then it could be implemented and turned off by default.

I propose that the exceptions thrown by unexpected and sorry are unchecked,
these are used with the programmer's understanding that either they are
temporary such as sorry or impossible such as unexpected.  (We could rename
unexpected.)  Some other errors such as domain errors should be unchecked.
All user-defined exceptions types should be checked.

There are other models for statically checking exceptions, such as OcamlExc
for Ocaml.  If anyone knows of a better model (that is either universally
better or fits Mercury better please let me know).


Paul Bone

More information about the developers mailing list