[Haskell-beginners] Why exception is not catching?!

Chris Sasarak csasarak at mailbox.org
Fri Dec 15 03:35:19 UTC 2017

I think that this might be related to where it says in the documentation that 'catch' must be called from inside the IO monad: http://hackage.haskell.org/package/base- That doc also points to the evaluate function for details on catching exceptions in pure code.

The reason it works in the GHCi repl is that GHCI is "special." Everything you evaluate it is in the IO monad/is implicitly in a 'do' block. This is why when you bind variables in GHCi you have to use 'let <defs>' rather than 'let <defs> in <expression>'. I think that what you're running into is that because your code is implicitly in an IO action in GHCi, you can catch the exception. In compiled code (I assume this is where it's breaking?) it's a pure function though.

I'd like for someone else to chime in though, at the least maybe look into using evaluate.


Baa writes:

> Hello, dear List!
> Super simple function - "safe enum conversion". It's based on `Either`
> because only it instantiates `MonadCatch` (Maybe does not, but with
> MaybeT I've got error that internal monad Indetity is not MonadCatch,
> so I avoid to use Maybe). That it is:
>   data A = A1|A2 deriving (Enum, Show)
>   data B = B1|B2|B3 deriving (Enum, Show)
>   conv :: B -> Either SomeException A
>   conv b = safeConv
>     where catchError (e::SomeException) = Left e
>       safeConv = (Right $ toEnum $ fromEnum b) `catch` catchError
>       -- toRes (Left _) = Nothing
>       -- toRes (Right r) = Just r
> But exception is not catching and I'm getting it in GHCI repl :)
> Exceptions are different?! What's wrong with this code?
> ===
> Best regards, Paul
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

More information about the Beginners mailing list