Question
· 10 hr ago

Try catch throwing error not actually throwing error to DTL in test or when run via router

Trying to check date in DTL. 

Want to throw a custom error not the default one from the $ZDTH. It does in the try as will write "Error" when testing the DTL but the actual error not showing. Report errors is turned on.  I also tried the THROW logic from best practices but that doesn't work either. $SYSTEM.Status.DisplayError(status) does display what the error should be returned. 

 try 
 {
    set tDateH = $ZDATEH(tDateofDeath, 8)
 }
 catch ex 
 { 
  w "error"
 set status = $$$ERROR($$$GeneralError,"Custom error message")
 do $SYSTEM.Status.DisplayError(status)
 do $System.Status.DecomposeStatus(status)  
    quit  
 }
Product version: IRIS 2024.1
Discussion (1)2
Log in or sign up to continue

There are some considerations when throwing custom errors in DTL through ObjectScript, especially within TRY-CATCH blocks:

1. Handling Errors with TRY-CATCH

  • When using the TRY-CATCH block, note that exceptions must populate an exception object with proper error details to be handled by the CATCH block. For example, you can create a custom exception object as an instance of the %Exception.General class and populate its fields such as Name, Code, and Data. Then, use the THROW statement within the TRY block to pass the object to the CATCH block for further handling [1][2].

2. THROW with Custom Exception Object

The THROW object must be properly instantiated. Example:

set ex = ##class(%Exception.General).%New("Custom Error", 999, , "This is a custom error message")
THROW ex

Use the CATCH block to handle the error and extract details like Name, Code, and Data from the thrown object for debugging your DTL processing [1][3].

3. System-Generated Exception Behavior

System-generated exceptions or errors occurring within a TRY block automatically create an instance of the %Exception.SystemException class. To create custom exceptions, you need to explicitly use the THROW object, as shown above [1][3].

4. $SYSTEM.Status.DisplayError(status)

Using $SYSTEM.Status.DisplayError(status) is suitable for displaying error status, but ensuring custom error handling requires proper creation and THROW of exception objects rather than relying solely on Status display [2][1].

5. $ZTRAP Integration

The $ZTRAP mechanism provides an alternative for implementing custom error handling. You can set $ZTRAP before entering the TRY block or within the CATCH block to manage additional error handling capabilities [2].

Reference the related documentation for more details:
- $ZTRAP usage specifics [4][2][5].
- $ZDATETIMEH error handling considerations [6][7].
- More examples regarding THROW with exception handling [1][3].

Sources: