[Haskell-beginners] Why exception is not catching?!
aquagnu at gmail.com
Fri Dec 15 08:47:24 UTC 2017
Hello, Chris! Yes, you are right. Gabriel answered already too:
What I can't understand in this case is: if partial functions CAN raise
"IO" exception CallError (I'm not sure in terminology here), and this
exception can be catched only in IO monad, why is this not reflected in
the types?! So, seems Haskell is not pure functional language, if there
are such surprices. Sure, there are compromises but they can be
reflected in types/signatures in some way :) Strange.
Solution may be to use "spoon" package which uses `deepseq` to force to
normal form but needs data to be NFData... Thanks to Gabriel for
Best regards, Paul
> I think that this might be related to where it says in the
> documentation that 'catch' must be called from inside the IO monad:
> 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
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners