How to fix (type) foreplay exceptions?

Say I'm doing the following from ghci:

   Prelude Control.Exception Data.Typeable> let a = read "A" :: Int
   Prelude Control.Exception Data.Typeable> a
   *** Exception: Prelude.read: no parse

Fine! Now I just need to somehow find out the type (and module) of this exception in order to write an exception handler. Is there a way to get the specified type and module?

+4
source share
2 answers

Built based on Daniel Wagner's answer:

import Control.Exception
import Data.Typeable

whichException :: IO a -> IO ()
whichException act = do
  e <- try act
  case e of
    Left (SomeException ex) -> print $ typeOf ex
    _                       -> putStrLn "No exception occurred"

-- Usage:
-- > whichException (evaluate (read "A"::Int))
-- ErrorCall
+8
source

, read Prelude. Prelude read Hackage, . , , read - error - errorCallException - ErrorCall , catch - GHC.Exception.ErrorCall. ghci:

> try (evaluate (read "A")) :: IO (Either ErrorCall Int)
Left Prelude.read: no parse

, !

+4

Source: https://habr.com/ru/post/1614422/


All Articles