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
}Comments
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-CATCHblock, 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.Generalclass and populate its fields such asName,Code, andData. Then, use theTHROWstatement 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:
Can you please provide some context on how/where you are using try/catch within DTL?
What exactly you want to happen when an error occur?
Depending on your requirements, maybe writing a custom function is easier to handle this situation?
In a "code" block
Basically within it trying to error the transform when date invalid
I'm not totally clear on what you're trying to do, but I have a feeling you might want to replace "quit" with "return status". Inside a catch block, a quit command will quit just the catch block and code execution will continue after it. Replacing that with return status would make the code execution stop right there, returning the error.
I don't think Try/Catch is the solution, but I don't know what you want to happen when an error occur.
Maybe an option could be using the "erropt" parameter in $ZDTH, like:
Set dateH=$ZDTH(whatevervalue,,,,,,,,,"invalid")
If dateH="invalid" ....