[Haskell-cafe] What is the surefire way to handle all exceptions and make sure the program doesn't fail?

Yifan Yu yvifan at gmail.com
Tue Jul 17 08:34:35 CEST 2012

First of all, apologise if the question is too broad. The background goes
like this: I've implemented a server program in Haskell for my company
intended to replace the previous one written in C which crashes a lot (and
btw the technology of the company is exclusively C-based).  When I chose
Haskell I promised my manager (arrogantly - I actually made a bet with
him), "it won't crash". Now it has been finished (with just a few hundred
LOC), and my test shows that it is indeed very stable. But by looking at
the code again I'm a little worried, since I'm rather new to exception
handling and there're many networking-related functions in the program. I
was tempted to catch (SomeException e) at the very top-level of the program
and try to recursively call main to restart the server in case of any
exception being thrown, but I highly doubt that is the correct and
idiomatic way. There are also a number of long-running threads launched
from the main thread, and exceptions thrown from these threads can't be
caught by the top-level `catch' in the main thread. My main function looks
like this:

main :: IO ()
main = withSocketsDo $ do
    sCameraU <- socketNewPassive False 6000
    sStunU   <- socketNewPassive False 3478
    sCmdT    <- socketNewPassive True  7000
    mvarCam  <- newMVar M.empty
    mvarLog  <- newMVar []

    forkIO $ regCamera sCameraU mvarCam mvarLog
    forkIO $ updCamera mvarCam mvarLog
    forkIO $ stun sCameraU sStunU mvarCam mvarLog

    listen sCmdT 128
    processCmd sCmdT mvarCam mvarLog

    sClose sCameraU
    sClose sStunU
    sClose sCmdT

I find that I can't tell whether a function will throw any exception at
all, or what exceptions will be thrown, by looking at their documentation.
I can only tell if I browse the source code. So the question is, how can I
determine all the exceptions that can be thrown by a given function? And
what is the best way to handle situations like this, with both the
long-running threads and main thread need to be restarted whenever
exceptions happen.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120717/927ee646/attachment.htm>

More information about the Haskell-Cafe mailing list