control-monad-exception-monadstf: Explicitly typed, checked exceptions with stack traces

[ control, failure, library, monads, public-domain ] [ Propose Tags ]

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

Computations throwing different types of exception can be combined seamlessly.


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

In addition to explicitly typed exceptions his package provides:

f () = do throw MyException
g a  = do f a

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

-- Running main produces the output:

*Main> main
   in f, Main(example.hs): (1,6)
      g, Main(example.hs): (2,6)
      main, Main(example.hs): (5,9)
      main, Main(example.hs): (4,16)
Versions [faq] 0.8.0,,,, 0.9, 0.9.1, 0.10.3
Dependencies base (>4 && <5), control-monad-exception (>=0.8.0), monads-tf (>=, transformers (>=0.1.0) [details]
License LicenseRef-PublicDomain
Author Pepe Iborra
Category Control, Monads, Failure
Home page
Source repo head: git clone git://
Uploaded by PepeIborra at 2009-12-17T11:25:19Z
Distributions NixOS:0.10.3
Downloads 4842 total (12 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs uploaded by user
Build status unknown [no reports yet]




Maintainer's Corner

For package maintainers and hackage trustees