Try Statement

Provides a way to catch run-time errors and handle errors locally before ATEasy proceed to handle errors using the module OnError event.

Syntax

Try

[ statements ]

[Catch [ Module modulename[,]] | [errornumber1 To Errornumber2 | ErrorNumber3[,...]]

 [ statements1 ]...]

[Catch Else

 [ statements2 ]]

EndTry

Comments

The Try-Catch block is used to localize the handling of any errors and exceptions which occur in a specific block of statements, which means that a specific response can be tailored to the purpose of the block of statements. For example, if an I/O table failed since the instrument was not responding, the statement can be used to catch the exception generated by ATEasy in order to reset the instrument and then to retry the failed command.

ATEasy executes the statements then, if and exception was generated during executing statements the first Catch block is tested if the exception matches the error numbers in the Catch blocks and or the modulename,  statements1 is executed or else the optional Catch Else statements2 are executed. After catch statements are executed the control goes to the statement following the EndTry.  If there is no match in any Catch statement in the current Try-Catch block, the caller Try-Catch block is checked. If all the active Try-Catch blocks are been checked without a match, the error is passed to the module OnError events.

The optional Module is used to catch errors only from the specified modulename. modulename can be Program, System, Driver or any of the current system drivers names (e.g. DMM). Error numbers are used to catch only specific error  numbers or range of numbers. If multiple error numbers or error range they must be separated by commas. Using the module block without the error numbers block allows you to catch all errors generated from the module.

Exceptions can be generated using the Error statement or when a run-time error occurs. Recoverable error are typically generated by ATEasy as result of run-time error that occur when Instrument is not responding or  COM objects methods or properties that failed when called. Errors raised by the Error statement are also recoverable. Other run-time errors such as divide by zero are non-recoverable (fatal).

You may use the Retry and Ignore statements to handle error in a catch block. Using Retry is only allowed for recoverable errors. If the error was a recoverable error, the error processing code after the Catch statement can execute an Ignore statement to continue execution immediately after where the error occurred or a Retry statement to transfer control back to the statement generated the error and repeat the statement. Using Ignore statement will resume control to the statement following the statement caused the error.

Alternatively, the error processing code can pass the error on to an outer Try block or to the Module OnError events. The Error statement with no parameters can be used to re-throw the error. In this case the error number, message, and location are unchanged. If the error was recoverable, a subsequent Ignore statement will continue from the original location of the error. The Error statement with an error number and optional error message clears the old error condition and creates a new one.

Try-Catch blocks can be explicitly nested, that is, one or more of the statements in statements1 can be a Try-Catch block. The more usual case is that one or more of the statements in statements1 is a call to a procedure which contains its own Try-Catch block(s) to deal with errors locally.

Example

The following is an example of a Try-Catch block which deals with DMM instrument commands:

iRetryCount = 0

Try

DMM Measure (dVolts)

Switch Open (1)

Catch Module DMM, 332, 333, 340 to 500

iRetryCount = iRetryCount + 1

If iRetryCount > 3 Then ExitTest

DMM Reset

Retry

Catch Module DMM ! other DMM errors

DMM Reset

   Ignore

Catch else       ! other errors e.g SWITCH

! other error

TestStatus=Fail

ExitTest

EndTry

See Also

Error, Ignore, Retry, OnError Event, GetErrorNum, GetErrorMsg, GetErrorModule