Yhc/RTS/Exceptions

From HaskellWiki
< Yhc‎ | RTS
Revision as of 18:37, 9 March 2007 by TomShackell (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

RTS Exceptions

Support for 'imprecise exceptions' has recently been added to Yhc. Imprecise exceptions allow any kind of exception (including 'error') to be thrown from pure code and caught in the IO monad.

This page attempts to describe how imprecise exceptions are implemented in the Runtime System.

The most important haskell functions for imprecise exceptions are 'catch' and 'throw'.

catch :: IO a -> (Exception -> IO a) -> IO a
throw :: Exception -> a

catch takes some IO code to run, and an exception handler and it runs the IO code and if an exception occurs it runs the exception handler.

throw simply throws an exception.

catch

catch is implemented using YHC.Exception.catchException

catchException :: IO a -> (Exception -> IO a) -> IO a
catchException io handler = IO $ \ w -> primCatch
                             (unsafePerformIO io)
                             (\e -> unsafePerformIO (handler e))

catchException simply converts from the IO monad into standard closures and passes them to the primitive 'primCatch'

primCatch :: a -> (b -> a) -> a

primCatch cannot be written in Haskell and is instead defined in bytecode as

primCatch e h

    CATCH_BEGIN handler
    PUSH_ZAP_ARG e
    EVAL
    CATCH_END
    RETURN
 handler:
    PUSH_ZAP_ARG h
    APPLY 1
    RETURN_EVAL