Skip to main content
Previous sectionNext section

Catch

Identifies a block of code to execute when an exception occurs.

Synopsis

Try
   statements
Catch [exceptionvar]
   statements
End Try

Arguments

exceptionvar Optional — An exception variable. Specified as a local variable, with or without subscripts, that receives a reference to a Caché Object.

Description

The Catch command defines an exception handler, one or more statements to execute when an exception occurs in the code following a Try statement. The Catch command 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 command has two forms:

  • Without an argument

  • With an argument

Catch without an Argument

Argumentless Catch execute the series of statements between Catch and End Try.

Catch with an Argument

Catch exceptionvar receives a Caché Object reference (oref) from the Throw command or from the system runtime environment in the event of a system error. This Object 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.

Arguments

exceptionvar

A local variable, used to receive the exception object reference from the Throw command or from the system runtime environment in the event of a system error. When a system error occurs, exceptionvar receives a reference to an object of type %Exception.SystemException. For further details, refer to the %Exception.AbstractException class in the InterSystems Class Reference.

Examples

The following example shows a Catch invoked by a runtime error. The myvar argument receives a system-generated exception object:

  Try 
    PRINTLN "about to divide by zero"
    SET a=7/0
    PRINTLN "this should not display"
  Catch myvar 
    PRINTLN "this is the exception handler"
    PRINTLN "Error is: ",Err.Description
    PRINTLN "Error code: ",myvar.Code
  End Try
  PRINTLN "this is where the code falls through"
Copy code to clipboard

See Also