The control-monad-exception package
This package provides explicitly typed, checked exceptions as a library.
Computations throwing different types of exception can be combined seamlessly.
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
This package provides, among other things:
Support for explicitly documented, unchecked exceptions (with tryEM).
Support for selective unchecked exceptions (with UncaughtException).
Support for exception stack traces (experimental). Example:
f () = $withLocTH $ throw MyException
g a = $withLocTH $ f a
main = runEMT $ $withLocTH $ do
g () `catchWithSrcLoc` \loc (e::MyException) -> lift(putStrLn$ showExceptionWithTrace loc e)
-- Running main produces the output:
*Main> main
MyException
in Main(example.hs): (12,6)
Main(example.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 |
|---|---|
| Dependencies | base (≥3.0 & <4), extensible-exceptions (0.1.*), monads-fd (0.0.*), pretty, template-haskell, transformers (≥0.0.1 & <0.2) or base (≥3.0 & <4), extensible-exceptions (0.1.*), mtl, pretty, template-haskell or base (4.*), monads-fd (0.0.*), pretty, template-haskell, transformers (≥0.0.1 & <0.2) or base (4.*), mtl, pretty, template-haskell |
| License | PublicDomain |
| Author | Pepe Iborra |
| Maintainer | pepeiborra@gmail.com |
| Stability | experimental |
| Category | Control, Monads |
| Home page | http://pepeiborra.github.com/control-monad-exception |
| Upload date | Tue Oct 27 18:00:33 UTC 2009 |
| Uploaded by | PepeIborra |
| Build failure | ghc-6.10 (log), ghc-6.12 (log) |
Modules
- Control
- Monad
- Control.Monad.Exception
- Control.Monad.Exception.Class
- Control.Monad.Exception.Throws
- Control.Monad.Exception
- Monad
Downloads
- control-monad-exception-0.4.7.tar.gz (Cabal source package)
- package description (included in the package)