[Haskell-cafe] How to catch exception within the Get monad (the Binary package)

Henning Thielemann schlepptop at henning-thielemann.de
Tue Sep 7 12:51:13 EDT 2010


Dimitry Golubovsky schrieb:
> Hi,
> 
> Thanks to everybody who replied.
> 
> I see another solution: are there any hidden problems?
> 
> I found an interesting package, ChasingBottoms which contains a
> function testing a value to be bottom and returning a Boolean (of
> course it cannot be done without unsafePerformIO).
> 
> I borrowed the idea from that package, and wrote two functions:
> 
> unThrow :: (Exception e) => a -> Either e a
> 
> unThrow a = unsafePerformIO $ (E.evaluate a >>= return . Right) `E.catch`
>                                                (\e -> return $ Left e)
> 
> -- or perhaps the right argument of catch could be just (return . Left)?
> 
> bm2mb :: a -> Maybe a
> 
> bm2mb a = case unThrow a of
>    Left (e::SomeException) -> Nothing
>    Right a -> Just a
> 
> So, if there is a value inside the lazy list which is a bottom (binary
> parse failure of the last received object in this case, catching any
> possible exception) then the value can be converted to Nothing within
> pure code, and then excluded from the result using catMaybes.
> 
> This solution seems to be working for me.

This solution looks very ugly to me. Catching 'error's is debugging, but
parser failure is kind of exception handling. I guess, the errors you
want to catch are caused by non-supported fail method, right? Can't you
use a monad transformer like explicit-exception:Synchronous.Exception or
transformers:ErrorT around the Binary parser?



More information about the Haskell-Cafe mailing list