# Is there a way to catch all exceptions?

**URL:** <https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609>\
**Category:** App Development\
**Tags:** daml\
**Created:** [December 20, 2021, 7:40pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609 "2021-12-20T19:40:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [December 20, 2021, 7:40pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609/1 "2021-12-20T19:40:02Z")

</div>

When I write

```haskell
try do
   ...
catch
  _ -> do
    ...

```

I get error

```haskell
Ambiguous type variable ‘e0’ arising from a use of ‘DA.Internal.Desugar.fromAnyException’
      prevents the constraint ‘(DA.Internal.Desugar.HasFromAnyException
                                  e0)’ from being solved.

```

Is there any way to catch all, or do I have to explicitly list all options?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [December 20, 2021, 8:00pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609/2 "2021-12-20T20:00:39Z")

</div>

~~No, you can only catch specific exception types atm.~~ see below

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [December 20, 2021, 8:11pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609/3 "2021-12-20T20:11:11Z")

</div>

Apologies, I blanked for a minute, that was completely wrong. You can catch `AnyException` to catch everything. To convert to a specific exception type afterwards use `fromAnyException`, .eg.

```haskell
module Main where

import DA.Assert
import Daml.Script

f : () -> Script Text
f () = 
  try do
    x <- fail "abc"
    pure "unreachable"
  catch
    (ex : AnyException) -> pure "caught"

test = do
  x <- f () 
  x === "caught"

```

---

<div class="post-metadata">

**Author:** ![Arne\_Gebert](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/arne_gebert/32/1213_2.png) [@Arne\_Gebert](https://forum.canton.network/u/Arne_Gebert)\
**Post date:** [October 18, 2022, 4:25pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609/4 "2022-10-18T16:25:17Z")

</div>

Is there a way to get the text of the exception when catching it generically?  
E.g., the following naive approach didn’t work:

```haskell
module Main where

import DA.Assert
import Daml.Script

f : () -> Script Text
f () = 
  try do
    x <- fail "abc"
    pure "unreachable"
  catch
    (ex : AnyException) -> pure ex.message

test = do
  x <- f () 
  x === "caught"

```

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [October 18, 2022, 4:51pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609/5 "2022-10-18T16:51:13Z")

</div>

`message` is not a record field here. It’s a method of the `HasMessage` typeclass implemented by all exceptions. So you want something like this:

```haskell
message ex

```

---

<div class="post-metadata">

**Author:** ![a\_putkov](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/a_putkov/32/2971_2.png) [@a\_putkov](https://forum.canton.network/u/a_putkov)\
**Post date:** [October 18, 2022, 5:01pm UTC](https://forum.canton.network/t/is-there-a-way-to-catch-all-exceptions/3609/6 "2022-10-18T17:01:23Z")

</div>

@Arne_Gebert  
To add to @cocreature 's reply, the value of x in your test will be “abc”, not “caught”. Try the following

```haskell
f : () -> Script Text
f () = do
  try do
    x <- fail "abc"
    pure "unreachable"
  catch
    (ex : AnyException) -> pure $ message ex

test : Script ()
test = do
  x <- f () 
  x === "abc"
  pure ()

```
