[Haskell-cafe] Catch multiple exceptions using 'Control.Exception'
Roman Cheplyaka
roma at ro-che.info
Sat Jul 6 23:06:54 CEST 2013
* 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?
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-types/
That's just different terminology.
When talking about Haskell, we usually call any algebraic type with
multiple constructors a sum type.
Roman
More information about the Haskell-Cafe
mailing list