My test case still shows "Unhandled exception" even if I try-catch-ed it?

Hi there,

Is this a bug when I try to write the test case - testRollback? Because the compiler would still complain that it’s a “Unhandled exception”. And furthermore when I run

daml test --show-coverage

, it’ll show the test case failed. Any idea from anyone? Thanks in advance.

data RollbackErrorCode = 
  VALIDATION_FAILED
  deriving (Eq, Ord, Show)

exception RollbackException
  with
    errorCode : RollbackErrorCode
    errorMsg : Text
  where
    message "RollbackException msg: errorCode - " <> show errorCode <> " , errorMsg - " <> show errorMsg

template TempRollback
  with
    a: Text
    owner: Party
  where
    signatory owner
  
    choice Test : ()
      controller owner
      do
        throw $ RollbackException VALIDATION_FAILED "abc"
        pure ()

testRollback = do
  alice <- allocateParty "Alice"
  cid <- submit alice do
    createCmd TempRollback with
      a = "aapple"
      owner = alice
  try do
    submit alice do
      exerciseCmd cid Test
  catch
    RollbackException _ _ ->
      pure ()
  pure ()
2 Likes

You might want to have a look here.

tl;dr try/catch is not currently supported at the top level in Daml Script and Daml Triggers, if you want to catch an error you have to do so as part of a transaction.

1 Like

Extending the answer given by Stefano, this is an example how you can wrap the try/catch in an adhoc contract: Testing the exact error message returned from failed transactions

1 Like

Thanks both!! submitMustFail works in my case.

2 Likes