TRY statements CATCH [exceptionvar] statements END TRY
|exceptionvar||Optional — An exception variable. Specified as a local variable, with or without subscripts, that receives a Caché Object reference (oref).|
The CATCH statement defines an exception handler, one or more statements to execute when an exception occurs in the code following a TRY statement. The CATCH statement is followed by one or more exception handling code statements. The CATCH block must immediately follow its TRY, and the paired TRY and CATCH are terminated by an END TRY statement.
The CATCH statement has two forms:
Without an argument
With an argument
CATCH without an Argument
Argumentless CATCH is invoked when an exception occurs in the TRY block. This executes the series of statements between CATCH and END TRY.
CATCH with an Argument
CATCH exceptionvar is invoked when an exception occurs in the TRY block. This exception passes exceptionvar to the CATCH block. This exception can either be explicitly invoked by a THROW statement, or issued by the system runtime environment in the event of a system exception. The exceptionvar Caché Object reference (oref) provides properties that contain information about the exception, such as the Name of the error and the Location where it occurred. The user-written CATCH exception handler code can use this information to analyze the exception.
A local variable, used to receive the exception object reference from the THROW statement or from the system runtime environment in the event of a system exception. When a system exception occurs, exceptionvar receives a reference to an object of type %Exception.SystemException. For further details, refer to the %Exception.AbstractExceptionOpens in a new tab class in the InterSystems Class Reference.
The following example shows a CATCH invoked by a runtime exception. The myvar argument receives a system-generated exception object:
TRY PRINT "about to divide by zero" a=7/0 PRINT "this should not display" CATCH myvar PRINT "this is the exception handler" PRINT :myvar->Name,"Error Name" PRINT :myvar->Code,"Error Code Number" PRINT :myvar->Location,"Error Location" END TRY PRINT "this is where the code falls through"