diff --git a/flatparse.cabal b/flatparse.cabal
--- a/flatparse.cabal
+++ b/flatparse.cabal
@@ -1,11 +1,11 @@
 cabal-version: 1.12
 
--- This file has been generated from package.yaml by hpack version 0.35.1.
+-- This file has been generated from package.yaml by hpack version 0.35.2.
 --
 -- see: https://github.com/sol/hpack
 
 name:           flatparse
-version:        0.5.0.0
+version:        0.5.0.1
 synopsis:       High-performance parsing from strict bytestrings
 description:    @Flatparse@ is a high-performance parsing library for strict bytestring input. See the README for more information:
                 <https://github.com/AndrasKovacs/flatparse>.
diff --git a/src/FlatParse/Basic.hs b/src/FlatParse/Basic.hs
--- a/src/FlatParse/Basic.hs
+++ b/src/FlatParse/Basic.hs
@@ -88,6 +88,7 @@
   , FP.Base.failed
   , FP.Base.try
   , FP.Base.err
+  , FP.Base.withError
   , FP.Base.fails
   , FP.Base.cut
   , FP.Base.cutting
diff --git a/src/FlatParse/Basic/Base.hs b/src/FlatParse/Basic/Base.hs
--- a/src/FlatParse/Basic/Base.hs
+++ b/src/FlatParse/Basic/Base.hs
@@ -40,6 +40,7 @@
   , failed
   , try
   , err
+  , withError
   , fails
   , cut
   , cutting
@@ -70,6 +71,14 @@
 err :: e -> ParserT st e a
 err e = ParserT \_fp _eob _s st -> Err# st e
 {-# inline err #-}
+
+-- | Run the parser, if an error is thrown, handle it with the given function.
+withError :: ParserT st e b -> (e -> ParserT st e b) -> ParserT st e b
+withError (ParserT f) hdl = ParserT $ \fp eob s st -> case f fp eob s st of
+  Err# st' e -> case hdl e of
+    ParserT g -> g fp eob s st'
+  x -> x
+{-# inline withError #-}
 
 -- | Convert a parsing error into failure.
 try :: ParserT st e a -> ParserT st e a
diff --git a/src/FlatParse/Stateful.hs b/src/FlatParse/Stateful.hs
--- a/src/FlatParse/Stateful.hs
+++ b/src/FlatParse/Stateful.hs
@@ -97,6 +97,7 @@
   , FP.Base.failed
   , FP.Base.try
   , FP.Base.err
+  , FP.Base.withError
   , FP.Base.fails
   , FP.Base.cut
   , FP.Base.cutting
diff --git a/src/FlatParse/Stateful/Base.hs b/src/FlatParse/Stateful/Base.hs
--- a/src/FlatParse/Stateful/Base.hs
+++ b/src/FlatParse/Stateful/Base.hs
@@ -40,6 +40,7 @@
   , failed
   , try
   , err
+  , withError
   , fails
   , cut
   , cutting
@@ -70,6 +71,14 @@
 err :: e -> ParserT st r e a
 err e = ParserT \_fp !_r _eob _s _n st -> Err# st e
 {-# inline err #-}
+
+-- | Run the parser, if an error is thrown, handle it with the given function.
+withError :: ParserT st r e b -> (e -> ParserT st r e b) -> ParserT st r e b
+withError (ParserT f) hdl = ParserT $ \fp !r eob s n st -> case f fp r eob s n st of
+  Err# st' e -> case hdl e of
+    ParserT g -> g fp r eob s n st'
+  x -> x
+{-# inline withError #-}
 
 -- | Convert a parsing error into failure.
 try :: ParserT st r e a -> ParserT st r e a
