The control-monad-exception package

[Tags:library, mit]

This package provides explicitly typed, checked exceptions as a library. Example

 data Expr = Add Expr Expr | Div Expr Expr | Val Double
 eval (Val x)     = return x
 eval (Add a1 a2) = do
    v1 <- eval a1
    v2 <- eval a2
    let sum = v1 + v2
    if sum < v1 || sum < v2 then throw SumOverflow else return sum
 eval (Div a1 a2) = do
    v1 <- eval a1
    v2 <- eval a2
    if v2 == 0 then throw DivideByZero else return (v1 / v2)
 data DivideByZero = DivideByZero deriving (Show, Typeable)
 data SumOverflow  = SumOverflow  deriving (Show, Typeable)
 instance Exception DivideByZero
 instance Exception SumOverflow

GHCi infers the following types

 eval :: (Throws DivideByZero l, Throws SumOverflow l) => Expr -> EM l Double
 eval `catch` \ (e::DivideByZero) -> return (-1)  :: Throws SumOverflow l => Expr -> EM l Double
 runEM(eval `catch` \ (e::SomeException) -> return (-1))  :: Expr -> Double

New in version 0.4: * (optionally) Unchecked exceptions (with UncaughtException) * Exception stack traces (with WithSrcLoc). Example

 f () = $withLocTH $ throw MyException
 g a  = $withLocTH $ f a

 main = runEMT $ $withLocTH $ do
 g () `catchWithSrcLoc` \loc MyException ->
       lift $ putStrLn (showExceptionWithTrace loc MyException)

Running main produces the output:

 *Main> main
 MyException
  in Main.docatch.hs: (12,6)
     Main.docatch.hs: (11,7)

Properties

Versions 0.1, 0.1.1, 0.1.2, 0.2, 0.3, 0.3.1, 0.3.2, 0.4, 0.4.1, 0.4.2, 0.4.3, 0.4.4, 0.4.5, 0.4.6, 0.4.7, 0.4.8, 0.5, 0.6, 0.8.0, 0.8.0.1, 0.8.0.2, 0.8.0.3, 0.8.0.4, 0.9.0, 0.9.1, 0.10.0, 0.10.1, 0.10.2, 0.10.3, 0.10.3.1, 0.11.1, 0.11.2
Dependencies base (>=3.0 && <5), extensible-exceptions (==0.1.*), monads-fd (==0.0.*), mtl, pretty, template-haskell, transformers (>=0.0.1 && <0.2) [details]
License PublicDomain
Author Pepe Iborra
Maintainer pepeiborra@gmail.com
Stability experimental
Category Control, Monads
Home page http://safe-tools.dsic.upv.es/mediawiki/index.php/Jose_Iborra/Papers/Exceptions
Uploaded Wed Sep 16 10:38:28 UTC 2009 by PepeIborra
Distributions NixOS:0.11.2
Downloads 6775 total (122 in the last 30 days)
Votes
0 []
Status Docs not available [build log]
All reported builds failed as of 2015-12-12 [all 5 reports]

Modules

  • Control
    • Monad
      • Control.Monad.Exception
        • Control.Monad.Exception.Class

Flags

NameDescriptionDefaultType
transformersUse transformers and monads-fd instead of mtlEnabledAutomatic
extensibleexceptionsUse extensible-exception packageDisabledAutomatic

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

For package maintainers and hackage trustees