Let's make the following assumptions:
How would I find out the type of that exception?
Minimal example:
main = error "foo"
(Here it's of course ErrorCall
, but you can't tell from the error message.)
The Haskell runtime system allows any IO action to throw runtime exceptions. Many common library functions throw runtime exceptions. There is no indication at the type level if something throws an exception. You should assume that, unless explicitly documented otherwise, all actions may throw an exception.
try :: Exception e => IO a -> IO (Either e a) try takes an IO action to run, and returns an Either . If the computation succeeded, the result is given wrapped in a Right constructor. (Think right as opposed to wrong). If the action threw an exception of the specified type, it is returned in a Left constructor.
Yes. All Exception
types must be instances of Typeable
, assuming you use the new exceptions API.
import Control.Exception
import Data.Typeable
import Prelude hiding (catch)
realMain = error "example"
main = realMain `catch` h where
h (SomeException e) = do
putStrLn $ "Caught exception of type " ++ show (typeOf e)
putStrLn $ show e
Results:
Caught exception of type GHC.Exception.ErrorCall example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With