Proposal: Add throwSTM and generalize catchSTM

Antoine Latter aslatter at
Sun Sep 26 12:13:54 EDT 2010

On Sun, Sep 26, 2010 at 10:24 AM, Bas van Dijk <v.dijk.bas at> wrote:
> Dear all,
> Currently the only way to throw an exception in an STM transaction is
> via throw. The IO monad has the throwIO function which guarantees
> ordering with respect to other IO actions. It would be nice to have a
> similar function for the STM monad:
> throwSTM :: Exception e => e -> STM a
> I propose adding this to Control.Monad.STM.
> Additionally I propose to generalize:
> catchSTM :: STM a -> (SomeException -> STM a) -> STM a
> to:
> catchSTM :: Exception e => STM a -> (e -> STM a) -> STM a
> to make it consistent with the IO catch function:
> catch :: Exception e => IO a -> e -> IO a -> IO a
> and allow packages like exception-monads-fd/tf to define an instance
> MonadException STM.
> I don't have a patch because I think these changes also require
> modifying/extending the rts which I don't feel comfortable with doing
> right now.

Sounds good to me.

Your new catchSTM can be written from the old one:

> newCatchSTM stm h = catchSTM stm (h . fromJust . fromException . toException)

The following definition for throwSTM typechecks, and does pretty much
exactly what you want, but we'll want someone on GHC to let us know
that it is not going to do weird things to the RTS:

> throwSTM e = STM (raiseIO# (toException e))

Attached is a file with the implementations of these and some examples of usage.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: STMError.hs
Type: application/octet-stream
Size: 670 bytes
Desc: not available
Url :

More information about the Libraries mailing list