TRY statements CATCH exceptionvar statements END TRY
The TRY statement takes no arguments. It is used to identify one or more Caché MVBasic code statements between the TRY keyword and the CATCH keyword. This block of code is protected code for structured exception handling. If an exception occurs within this block of code, Caché sets exceptionvar to an object describing the exception, then transfers execution to an exception handler, identified by the CATCH statement. This is known as throwing an exception. If no exception occurs, execution continues with the next Caché MVBasic statement after the END TRY statement.
An exception may occur as a result of a runtime exception, such as attempting to divide by 0, or it may be explicitly propagated by issuing a THROW statement.
A TRY block must be immediately followed by a CATCH block. The paired TRY and CATCH are terminated by an END TRY statement.
In the following examples, the TRY code block is executed. It attempts to set the local variable a. In the first example, the code completes successfully, and the CATCH statements are skipped over. In the second example, the code fails an Err error indicating division by zero, and execution is passed to the CATCH statement.
TRY PRINT "about to divide by one" a=7/1 PRINT "this line is executed" CATCH myvar PRINT "this is the exception handler" PRINT "Error name: ",myvar->Name END TRY PRINT "this is where the code falls through"
TRY PRINT "about to divide by zero" a=7/0 PRINT "this should not display" CATCH myvar PRINT "this is the exception handler" PRINT "Error name: ",myvar->Name END TRY PRINT "this is where the code falls through"