Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rethrow generic exception in Haskell

Given

f :: Int -> IO (Either SomeException Int)

g = do
  results <- mapM f [1,2,3]
  let (errs, succs) = partitionEithers results
  if null errs then return succs
               else  -- rethrow arbitrary exception
                     throwIO (fromJust . fromException $ head errs)

I would have expected g to rethrow whatever exception it encounters, however compilation fails with ambigous type variable. Sprinkling randomly with existential quantified helpers doesn't help.

How can I rethrow an arbitrary exception I encounter without loosing genericity?

like image 650
ron Avatar asked Jan 23 '26 14:01

ron


1 Answers

Just use throwIO $ head errs.

The error message is because fromException needs to go to a type known at compile time. You provide no context for it to figure out what type it is, so it complains that the type variable is ambiguous.

Fortunately, you don't need to know what type it is. SomeException is an instance of Exception, too. It can be rethrown without ever knowing what type it contains. In fact, if you look at the definition of the Exception class, it's just toException and fromException, which are conversion functions that lean on Typeable to ensure that they don't break the type system. SomeException's instance for Exception just has the most boring possible definitions for those.

like image 183
Carl Avatar answered Jan 25 '26 02:01

Carl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!