# Asserting Errors in Unit Test when thrown in a choice

**URL:** <https://forum.canton.network/t/asserting-errors-in-unit-test-when-thrown-in-a-choice/5831>\
**Category:** App Development\
**Created:** [December 16, 2022, 8:59am UTC](https://forum.canton.network/t/asserting-errors-in-unit-test-when-thrown-in-a-choice/5831 "2022-12-16T08:59:58Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [December 16, 2022, 7:25pm UTC](https://forum.canton.network/t/asserting-errors-in-unit-test-when-thrown-in-a-choice/5831/2 "2022-12-16T19:25:02Z")

</div>

The [Exception Handling](https://docs.daml.com/daml/intro/8_Exceptions.html) section in Daml documentation discusses two options for handling exceptions: using `Either` data type and using `try-catch` block. For your use case you need to utilize both.  
Here’s an example with a custom exception.

```haskell
exception ZeroAge
  where
    message "Age cannot be zero"

template Student
  with
    age : Decimal
    name : Text
    school : Party
  where
    signatory school

    choice SetAge : ContractId Student
      with
        newage : Decimal
      controller school
      do
        if newage == 0.0
        then throw ZeroAge 
        else create this with age = newage

template TestHelper 
  with
    tester : Party
  where
    signatory tester

    nonconsuming choice Test_SetAge : Either Text Text
      with
        studentCid : ContractId Student
        newage : Decimal
      controller tester
      do
        try do
          exercise studentCid SetAge with newage
          return $ Right "Success"
        catch 
          (ex : ZeroAge) -> do
            return $ Left $ message ex

testSetAge = script do
  hs1 <- allocateParty "High School 1"
  alice_studentCid <- submit hs1 do
    createCmd Student with
      age = -1.0
      name = "Alice"
      school = hs1
  thCid <- submit hs1 do
    createCmd TestHelper with
      tester = hs1
  test_result <- submit hs1 do
    exerciseCmd thCid Test_SetAge with 
      studentCid = alice_studentCid
      newage = 0.0
  test_result === Left "Age cannot be zero"
  return ()

```

Note that, because exceptions are not propagated across `submit`, you cannot catch the exception thrown by SetAge choice in a `try-catch` block in Daml Script, and you need to utilize a helper template to catch the exception. For details on why this is the case and some additional examples check out previous discussions on the following threads

> [@How do I catch an inbuilt Exception in a Script?](https://discuss.daml.com/t/how-do-i-catch-an-inbuilt-exception-in-a-script/2989):
>
> I’m trying to handle an inbuilt exception in a Script. Using the getting-started User example, I’ve written this test, trying to handle an assertion failure and just log the error message: my\_test : Script () my\_test = script do alice \<- allocateParty "Alice" bob \<- allocateParty "Bob" aliceUserCid \<- submit alice do createCmd User with username = alice; following = [] bobUserCid \<- submit bob do createCmd User with username = bob; following = [] aliceUserCid \<- submit alice…

> [@Negative Testing](https://discuss.daml.com/t/negative-testing/2988):
>
> Hey team, I’m writing some scripts to test some new features I’ve recently added to a Daml project. One thing I noticed is when I do negative testing, using submitMustFail that I’d like to also provide the reason why the submission should fail. Currently, I have to manually check that the submission failed as I cannot specify the reason why the submission should fail. For example, I have choices that requires multiple signatures with multiple assertions. I cannot distinguish a failure on a mis…

---

_[View the full topic](https://forum.canton.network/t/asserting-errors-in-unit-test-when-thrown-in-a-choice/5831)._
