Nikita Karetnikov | 3 Jul 12:47 2013

Catch multiple exceptions using 'Control.Exception'

I'm trying to update a package that uses 'Control.OldException' (works
with GHC 6.10.4).  Here is a relevant (and simplified) part:

import Control.OldException

-- | A predicate matching synchronous exceptions.
syncExceptions :: Exception -> Maybe Exception
syncExceptions (AsyncException _) = Nothing
syncExceptions e                  = Just e

throwAsync :: IO a
throwAsync = throwIO $ AsyncException StackOverflow

throwArith :: IO a
throwArith = throwIO $ ArithException DivideByZero

'syncExceptions' is usually used like this:

*Main> tryJust syncExceptions throwArith
Left divide by zero
*Main> tryJust syncExceptions throwAsync  -- pass through
*** Exception: stack overflow

The above doesn't work with GHC 7.6.3 because 'Control.OldException' [1]
was removed.  And 'Control.Exception' doesn't have the 'Exception' type.

Is there a way to adapt 'syncExceptions' to work with
'Control.Exception'?

[1] http://hackage.haskell.org/packages/archive/base/4.1.0.0/doc/html/Control-OldException.html
(Continue reading)

Erik Hesselink | 3 Jul 13:35 2013
Picon

Re: Catch multiple exceptions using 'Control.Exception'

Perhaps you can use `catches` [0]?

Erik

[0] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exception.html#v:catches

On Wed, Jul 3, 2013 at 12:47 PM, Nikita Karetnikov
<nikita <at> karetnikov.org> wrote:
> I'm trying to update a package that uses 'Control.OldException' (works
> with GHC 6.10.4).  Here is a relevant (and simplified) part:
>
> import Control.OldException
>
> -- | A predicate matching synchronous exceptions.
> syncExceptions :: Exception -> Maybe Exception
> syncExceptions (AsyncException _) = Nothing
> syncExceptions e                  = Just e
>
> throwAsync :: IO a
> throwAsync = throwIO $ AsyncException StackOverflow
>
> throwArith :: IO a
> throwArith = throwIO $ ArithException DivideByZero
>
> 'syncExceptions' is usually used like this:
>
> *Main> tryJust syncExceptions throwArith
> Left divide by zero
> *Main> tryJust syncExceptions throwAsync  -- pass through
> *** Exception: stack overflow
(Continue reading)

Nikita Karetnikov | 3 Jul 13:50 2013

Re: Catch multiple exceptions using 'Control.Exception'

> Perhaps you can use `catches` [0]?

Maybe, but my idea is to replace 'syncExceptions' with a similar
function.  Otherwise, it'll be necessary to change (at least) all
functions that use 'syncExceptions'.  I'd like to avoid that.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 4 Jul 00:17 2013

Re: Catch multiple exceptions using 'Control.Exception'

* Nikita Karetnikov <nikita <at> karetnikov.org> [2013-07-03 15:50:16+0400]
> > Perhaps you can use `catches` [0]?
> 
> Maybe, but my idea is to replace 'syncExceptions' with a similar
> function.  Otherwise, it'll be necessary to change (at least) all
> functions that use 'syncExceptions'.  I'd like to avoid that.

Here you go:

  import Control.Exception
  import Data.Typeable

  syncExceptions :: SomeException -> Maybe SomeException
  syncExceptions e
    | Just _ <- cast e :: Maybe AsyncException = Nothing
    | otherwise = Just e

Roman
Nikita Karetnikov | 6 Jul 18:12 2013

Re: Catch multiple exceptions using 'Control.Exception'

> Here you go:

>   import Control.Exception
>   import Data.Typeable

>   syncExceptions :: SomeException -> Maybe SomeException
>   syncExceptions e
>     | Just _ <- cast e :: Maybe AsyncException = Nothing
>     | otherwise = Just e

Thanks, but it doesn't work as expected:

*Main> syncExceptions $ toException StackOverflow
Just stack overflow

It should return 'Nothing' instead:

*Main> cast $ toException StackOverflow :: Maybe AsyncException
Nothing

I've received another solution, and it works fine.  I also have other
questions (see below).

-------------------- Start of forwarded message --------------------
From: "Albert Y. C. Lai" <trebla <at> vex.net>

[...]

import Control.Exception

(Continue reading)

Roman Cheplyaka | 6 Jul 23:06 2013

Re: Catch multiple exceptions using 'Control.Exception'

* Nikita Karetnikov <nikita <at> karetnikov.org> [2013-07-06 20:12:58+0400]
> > Here you go:
> 
> >   import Control.Exception
> >   import Data.Typeable
> 
> >   syncExceptions :: SomeException -> Maybe SomeException
> >   syncExceptions e
> >     | Just _ <- cast e :: Maybe AsyncException = Nothing
> >     | otherwise = Just e
> 
> Thanks, but it doesn't work as expected:
> 
> *Main> syncExceptions $ toException StackOverflow
> Just stack overflow
> 
> It should return 'Nothing' instead:
> 
> *Main> cast $ toException StackOverflow :: Maybe AsyncException
> Nothing
> 
> I've received another solution, and it works fine.  I also have other
> questions (see below).

Ah yes, fromException is the right function, of course. Sorry for
confusion.

> Could anyone elaborate on the following terms: "a closed sum type" and
> "an open existential type"?  What is the meaning of the words "open" and
> "closed"?  Is there an open sum type or a closed existential type?
(Continue reading)

Albert Y. C. Lai | 7 Jul 19:38 2013
Picon

Re: Catch multiple exceptions using 'Control.Exception'

On 13-07-06 12:12 PM, Nikita Karetnikov wrote:
> Is there an open sum type or a closed existential type?

OCaml has open sum types.

I haven't really seen closed existential types. (I have seen some 
approximations, but each lacks one last bit.)

Gmane