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

Donn Cave donn at avvanta.com
Sun Sep 5 13:50:00 EDT 2010

Quoth Dimitry Golubovsky <golubovsky at gmail.com>,

> Is there any way to catch/detect failures inside the Get monad? It is
> not an instance of MonadError, so catchError does not work.
> Ideally, the function would keep decoding as long as it is possible,
> and upon the first failure of the parser, return whatever has been
> decoded.

I believe it can't be done.  (I've seen three responses that seemed
to be proposing some course of action, but ... correct me if I'm wrong,
nothing that would allow you to use Get in Data.Binary this way.)

The key point is the use of `throw', via `error', in Get's `fail'.
`throw' raises an exception that can be caught only in IO, so you
can't catch it inside Get.  So ... while `fail' is a Monad function,
it isn't implemented here in a way you could use, like it is in Maybe
for example.  Nor could it be, I think, which is kind of unfortunate.

	Donn Cave, donn at avvanta.com

More information about the Haskell-Cafe mailing list