[Haskell-cafe] How to catch all exceptions that could be caught?

Gregory Crosswhite gcrosswhite at gmail.com
Thu Jan 12 08:49:27 CET 2012


On 01/12/12 16:58, Magicloud Magiclouds wrote:
> Yes, that is a problem. But consider my PS in original mail, I have no
> idea what exception should I catch. Where could I get that
> information?

In my experience, exceptions fall into three categories.

First, when performing IO, some functions throw an exception instead of
returning an error code, as is the case for many of the functions in
System.IO;  however, in these cases the exceptions that can be thrown
are clearly documented.

Second, when a bug in your code has caused it to reach a point where it
can no longer proceed, such as when there is a deadlock, when there is
an infinite loop, when you violated a precondition of a pure function by
for example calling "head" on an empty list, etc.;  in such cases it is
very unlikely that you would even want to catch and gracefully recover
from them, so an exception specification would not help you very much
anyway.

Third, when you are running someone else's code and you want to be able
to catch any exceptions it throws so that you can handle the error
reporting yourself; in this case the only thing that you care about is
whether the exception is an AsyncException or not, since if it is an
AsyncException then you should almost certainly should just let it
propagate up the call stack rather than dealing with it yourself.

Incidentally, in all of these cases catching *all* exceptions is a bad
idea unless you really know what you are doing for the reasons that
others here have pointed out, so I apologize for misleading you with
that suggestion.

Cheers,
Greg



More information about the Haskell-Cafe mailing list