Catching errors in a transaction

With the catchError script operation, you can trap the underlying issues and prevent an error that causes the entire script to fail.

Do not use catchError without a matching throwError statement. Also, do not use start*!ENT!* and catchError together. The rollback of the transaction occurs only if the end brace of the useTransaction sees the exception. If the exception was caught and not rethrown in the block, then the rollback does not occur and there is the potential of corrupting the database.

When used around a transaction operation, such as an entity save, the following script operations can cause errors during the save to go unhandled. You need to either avoid including such operations within a catchError block, or be sure to rethrow the error after resolving it yourself. It might be simpler to refactor the script logic to avoid including the operations within it.
Table 1. Example of a bad catchError usage
Example of a bad catchError usage Example of a throwError use
useTransaction{
    catchError(e){
      saveCtgItem(...)
    }
    if(e!=null)
    {
      ... handle error .. 
    }
  }
useTransaction{
    catchError(e){
      saveCtgItem(...)
    }
    if(e!=null)
    {
      ... handle error ..
      throwError(e); 
    }
  }
As an alternative, if you are controlling the transaction, you can change the order and enclose the useTransaction within the catchError. For example,
catchError(e){
    useTransaction{
      saveCtgItem(...)
    }
  }
  if(e!=null)
  {
    ... handle error ..
  }