Try and Catch

The Try-Catch block is used to localize the handling of exceptions in ATEasy. You can use the Try-Catch block to wrap code in which you foresee potential errors, and catch different possible error conditions as they happen. By this means, you can track down errors to specific parts of your program.

For example, you might use a Try-Catch block to monitor a floating point calculation which is likely to result in an overflow or underflow, or to monitor communication with an instrument which is likely to time out or generate other errors.

Since the Try-Catch block is used to provide very specific responses to some or all of the errors that occur in the code comprising the Try block, all errors can be trapped by the Catch statements.

For example, retrying an "Unable to open file error" will not usually resolve the error. However, if the code to open and read a file of driver initialization data is in a Try block, then the file-open error could be caught and default values could be supplied for the driver parameters.

The general form of a Try-Catch block is as follows:

 

Try

statement 1

...

statement n

Catch [error number list 1]

Statements to handle errors listed in list 1

Catch [error number list 2]

Statements to handle errors listed in list 2

EndTry

The first indented block (statement 1... statement n) after Try contains the code that will be tested using the Try-Catch block. The first Catch block tests for a list of different possible error codes. If the error in the Try block is found in this list, the statements after the first Catch statement and before the next Catch statement are executed. These statements will handle the error condition signified by the error number(s) throwing the exception. If the error is handled in this way, it is assumed that the handling is successful, and execution passes to the EndTry statement.

Alternatively, the error handling code could execute a Retry or Ignore statement to go back to the start of the statement that caused the error (Retry) or to the next statement after the Try-Catch block (Ignore). If the error is not listed in the first Catch statement, then execution proceeds to the next Catch statement.

If no Catch statement in any active Try block matches the error, then the normal OnError() event processing is started.

The catch block is the only way to completely reset an error condition, regardless of what error occurred. If the flow of control reaches the end of the catch block (the next catch statement or the EndTry statement), the error condition is cleared and execution continues after the EndTry statement.