[Haskell-cafe] Absolutely confused with error/exception handling

Matt parsonsmatt at gmail.com
Fri Dec 16 03:39:56 UTC 2016


Hi Saurabh,

I've found that the `safe-exceptions` package [1] and related articles [2]
to be some of the best reading around exceptions in Haskell.

To answer your third question, the `IO` instance for
`MonadThrow`/`MonadCatch`/etc. do use Haskell's extensible exception
machinery. The monad-ste [3] package uses Haskell's exception hierarchy in
a controlled manner where evaluating `Either` ends up being expensive.

I've also found exceptions to be one of the hardest parts about Haskell.
Interestingly, they're one of the few dynamically typed bits with nearly
OO-style inheritance.

[1]: https://hackage.haskell.org/package/safe-exceptions
[2]: https://haskell-lang.org/tutorial/exception-safety
[3]: https://hackage.haskell.org/package/monad-ste

Matt Parsons

On Thu, Dec 15, 2016 at 10:30 PM, Saurabh Nanda <saurabhnanda at gmail.com>
wrote:

> Thanks. The air is clearing up a bit.
>
> Any reason for "error" and "catch" to be in different packages? Or, why
> isn't "catch" in prelude?
>
> "catch" needs a action in IO. What if I have an action in MonadIO?
>
> -- Saurabh.
>
> On 16 Dec 2016 8:00 am, "Brandon Allbery" <allbery.b at gmail.com> wrote:
>
>> whoops, pressed wrong key...
>>
>> No guarantees are made as to when the exception thrown by `throw` occurs;
>> this allows it to be thrown from pure code. `throwIO` is sequenced as any
>> other I/O operation, so you have some guarantee as to I/O operations before
>> the `throwIO` being performed.
>>
>> On Thu, Dec 15, 2016 at 9:28 PM, Brandon Allbery <allbery.b at gmail.com>
>> wrote:
>>
>>>
>>> On Thu, Dec 15, 2016 at 8:42 PM, Saurabh Nanda <saurabhnanda at gmail.com>
>>> wrote:
>>>
>>>> * What exactly is done by the "error" function? How does one "trap"
>>>> those errors and react to it? Can "catch" trap those errors? What is "e" in
>>>> the case of errors raised by "error"?
>>>>
>>>
>>> `error` specifically raises a UserError exception, with a user-specified
>>> payload (message). `catch` can catch them as such.
>>>
>>>
>>>> * How is "error" different from "throw" and "throwIO" (in the
>>>> Control.Exception package, I believe)
>>>>
>>>
>>> `throw` (for pure code / asynchronous) and `throwIO` (for code in IO)
>>> can throw any exception. No guarantees are made as to when the exception
>>> thrown by `throw`
>>>
>>>
>>> --
>>> brandon s allbery kf8nh                               sine nomine
>>> associates
>>> allbery.b at gmail.com
>>> ballbery at sinenomine.net
>>> unix, openafs, kerberos, infrastructure, xmonad
>>> http://sinenomine.net
>>>
>>
>>
>>
>> --
>> brandon s allbery kf8nh                               sine nomine
>> associates
>> allbery.b at gmail.com
>> ballbery at sinenomine.net
>> unix, openafs, kerberos, infrastructure, xmonad
>> http://sinenomine.net
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20161215/baa24090/attachment.html>


More information about the Haskell-Cafe mailing list