# Handling DAML \`error\`s

**URL:** <https://forum.canton.network/t/handling-daml-error-s/284>\
**Category:** App Development\
**Created:** [April 28, 2020, 2:50pm UTC](https://forum.canton.network/t/handling-daml-error-s/284 "2020-04-28T14:50:21Z")\
**Posts on this page:** 8\
**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:** [April 28, 2020, 2:50pm UTC](https://forum.canton.network/t/handling-daml-error-s/284/1 "2020-04-28T14:50:21Z")

</div>

Would it be possible to add a function

```haskell
try : (ActionFail m) => a -> m a

```

to DAML, which turns an erroneous expression into a failing action using the same message?

If not, why not?

---

<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:** [April 28, 2020, 3:20pm UTC](https://forum.canton.network/t/handling-daml-error-s/284/2 "2020-04-28T15:20:39Z")

</div>

There is no way to catch a call to `error` in DAML , it will abort the transaction. If you want catchable errors you can use something like `Either Text a` where you represent errors as `Left "error"` and success as `Right a`. Then you can handle the error simply by pattern matching. Note that a lot of methods, e.g., `abort` are overloaded so you can use `abort "foo"` in `Either Text` and it will produce a `Left "foo"`. So if you don’t want to decide upfront how errors are handled, you can write your code with a `CanAbort` constraint and then later decide which instance you use.

---

<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:** [May 4, 2020, 8:05am UTC](https://forum.canton.network/t/handling-daml-error-s/284/3 "2020-05-04T08:05:24Z")

</div>

I know there isn’t a way to do it right now. But would there by any issues if one added such a function to LF? My thinking is that it would give an easy way to handle errors such as division by zero.

---

<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:** [May 4, 2020, 8:23am UTC](https://forum.canton.network/t/handling-daml-error-s/284/4 "2020-05-04T08:23:08Z")

</div>

I don’t see an issue with adding such a function. Of course you can argue about how useful it would be.

---

<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:** [May 4, 2020, 8:39am UTC](https://forum.canton.network/t/handling-daml-error-s/284/5 "2020-05-04T08:39:46Z")

</div>

Note that you run into strictness issues if `try` is simply a function. So you have to be somewhat careful when implementing it.

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [May 4, 2020, 11:37am UTC](https://forum.canton.network/t/handling-daml-error-s/284/6 "2020-05-04T11:37:21Z")

</div>

In strict languages `try` is one of a handful of operators (along with `if`, `and` and `or`, as examples) that are not strict (and thus also not functions).

Edit: `s/lazy/strict/`

---

<div class="post-metadata">

**Author:** ![SamirTalwar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/samirtalwar/32/374_2.png) [@SamirTalwar](https://forum.canton.network/u/SamirTalwar)\
**Post date:** [May 5, 2020, 12:33pm UTC](https://forum.canton.network/t/handling-daml-error-s/284/7 "2020-05-05T12:33:06Z")

</div>

Do you mean “are lazy” rather than “are not lazy”? I’d be very confused if `if` was eager.

---

<div class="post-metadata">

**Author:** ![rohanjr](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/rohanjr/32/259_2.png) [@rohanjr](https://forum.canton.network/u/rohanjr)\
**Post date:** [May 5, 2020, 3:05pm UTC](https://forum.canton.network/t/handling-daml-error-s/284/8 "2020-05-05T15:05:29Z")

</div>

At least theoretically, I learnt that `if` is sugar for a `case` expression, i.e.

```haskell
if b then x else y ===
case b of
  True -> x
  False -> y

```

There are implicit binders around the `x` and `y` branches (since `True` and `False` have implicit unit arguments) which means they aren’t evaluated “eagerly” even in strict languages like OCaml. This is the word of Bob Harper anyway. I think you can use the same reasoning for `and` and `or` (though the actual implementation would be different).
