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-Ol...

Perhaps you can use `catches` [0]?
Erik
[0] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exc...
On Wed, Jul 3, 2013 at 12:47 PM, Nikita Karetnikov
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-Ol...
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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.

* Nikita Karetnikov
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

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"

* Nikita Karetnikov
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?
Closed means that the set of possible constructors is, well, closed — you have to decide on it when you're defining your type and you can't later add anything to it. Open means the opposite. A classical example of an open type is any type in an OO language with subtyping, such as Java. You can extend the Object type without limitations, without modifying the Object type itself. Exceptions are very similar in this regard — you can add new classes of exceptions without a need to mess with the Control.Exception module, just by defining a couple of methods. For more details you may want to read "An Extensible Dynamically-Typed Hierarchy of Exceptions".
Also, I thought that a sum type [1] should only have two value constructors:
...
[1] http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-...
That's just different terminology. When talking about Haskell, we usually call any algebraic type with multiple constructors a sum type. Roman

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.)
participants (4)
-
Albert Y. C. Lai
-
Erik Hesselink
-
Nikita Karetnikov
-
Roman Cheplyaka