Proposal: make throwIO and throw strict
David Feuer
david.feuer at gmail.com
Mon Feb 27 20:42:03 UTC 2017
It's possible for code to throw an exception that itself throws an
imprecise exception. Such an exception is a bit tricky to catch. For
example:
import Control.Exception
strange = throwIO (undefined :: SomeException) `catch` \ex ->
case () of
_ | Just _ <- (fromException ex :: Maybe IOError) -> print "IOError"
| otherwise -> print "Something else"
You might think that this would catch the exception and print
"Something else", but in fact it does not. If others think this is as
surprising as I do, then I think we should make throwIO and throw
strict, so an exception will never itself be bottom. Using
throwIO' !e = throwIO e
in the code above instead of throwIO allows the exception to be caught.
A more conservative approach might be to just force result of
toException before calling raise#, but this only works when users use
an explicit type signature to fix the expression type, rather than an
exception constructor.
David
More information about the ghc-devs
mailing list