In automated systems, it is typically very important that the equipment be able to run unattended for long periods of time. Since errors and other unexpected events periodically occur, it is critical that the system be able to automatically field execution exceptions, attempt to correct the problem by responding in an appropriate manner, and continue execution if at all possible. In GPL, sections of procedures or entire procedures can be bounded by a Try...Catch...Finally...End Try structure that provides a formal means to intercept program exceptions and execute specific corrective actions. When an exception is handled in this manner, information on the type of exception is stored in an Exception Object.
In the group of instructions shown below, if an exception of any type occurs when the try_statements are executed, rather than halting execution and reporting an error, the system automatically stores the exception information in the exception_object and branches execution to the start of the catch_statements. The catch_statements can test the exception_object to determine the nature of the exception and then perform whatever corrective action is necessary. If the try_statements complete execution without an error or when the catch_statements complete execution after an exception, the finally_statements are always executed to perform any required cleanup. At the completion of the finally_statements, regular instruction execution continues at the first statement following the End Try.
Try
try_statements
Catch exception_object
catch_statements
Finally
finally_statements
End Try
A Try structure must contain either a single Catch statement or a single Finally statement or one of each type of statement. If a Catch statement is specified, it must always include an exception_object.
Try structures can be nested within each other. For example, a Try structure can be contained within the catch_statements of another, higher-level Try structure. Also, procedure calls can be contained within any of the statement blocks including the try_statements. For example,
Public Sub MAIN
Dim exc1 As New Exception
Try
test()
Console.WriteLine("Test completed") ' Never gets here
Catch exc1
Console.WriteLine("Exception!") ' Is executed
End Try
End Sub
Public Sub test()
Dim ii As Integer
ii = 1 / 0 ' Generates exception
Console.WriteLine("Inside Test") ' Never gets here
End Sub
In this sample code, the only output will be "Exception!". This is because the divide by 0 in test generates an exception, which terminates execution of test. If the call to test in the MAIN routine was not embedded within a Try, the system would normally halt the execution of the thread and report the error. Since the call is within a Try block that has a Catch, execution is instead continued at the first instruction within the Catch block. This feature permits exceptions that occur within arbitrary depths of procedure calls to be fielded by a single Try structure.
A Try structure with a Finally instruction and no Catch instruction is only useful in a called procedure when a higher-level calling procedure contains a Try structure with a Catch. When an exception occurs in the try_statements of a called procedure with no Catch, the finally_statements are executed before the procedure exits to the higher-level procedure that contains the Catch statement. In the example above, if the divide by 0 statement was part of a Try block that was followed by a Finally block, the statements in the Finally block would have been executed prior to returning to the MAIN routine.
There are special limitations on the use of GoTo instructions in connection with Try structures. A GoTo contained in the catch_statements can branch execution into the corresponding try_statements. Also, GoTo's can be contained in the try_statements, catch_statements, and the finally_statements so long as the branch is to an instruction within the same block of statements. All other branching into and out of the Try statement blocks and the main code is not permitted, e.g. you cannot branch from outside of a Try structure into the try_statements or out of the try_statements into the finally_statements. For example,
Dim exc1 As New Exception
Try
retry:
Move.Loc(loc1, profile1)
Move.WaitForEOM
Catch exc1
If (exc1.ErrorCode = -153) Then
profile1.Speed *= .9
GoTo retry ' LEGAL BRANCH
End If
GoTo bad_jump ' ILLEGAL!!!
End Try
bad_jump:
If an Exit Try statement is executed in either the try_statements or the catch_statements, execution branches and continues at the first statement in the finally_statements. Exit Try instructions are not permitted in the finally_statements.
The Throw statement can be used to force an exception within a program at any time. The syntax for this instruction is as follows:
Throw exception_object
In addition to forcing an exception to halt program execution, the Throw statement is often used within a catch_statements block to force an exception to be processed by a higher-level Try structure.
Whenever an exception occurs, the data that defines the specific type of exception is stored and passed in Exception Objects. There are two basic types of Exceptions: robot Exceptions and general Exceptions. Both forms have a numeric property that indicates the basic type of error. In addition, the robot Exceptions contain information on the robot and axis that is associated with the Exception. The general Exceptions contain an error code qualifier in place of the robot and axis information.
As with other types of Objects, Exception Objects are defined with a Dim statement or as an argument to a procedure. When an Exception Object is first created, normally the New token is used to allocate the data section for the Object.
All of the properties and methods for the Exception Objects are described in detail in the Reference Documentation section. Table 19-11 summarizes this information.
| Member | Type | Description |
|---|---|---|
|
exception_obj.Axis |
Property |
Sets and gets a bit mask indicating the robot axes associated with a robot Exception. |
|
exception_obj.Clone |
Method |
Method that returns a copy of the exception_obj. |
|
exception_obj.ErrorCode |
Property |
Sets and gets the number of the error message. |
|
exception_obj.Message |
Method |
Returns the full text string that is generated based upon the exception_obj properties. |
|
exception_obj.Qualifier |
Property |
Sets and gets the error message qualifier for a general Exception. |
|
exception_obj.RobotError |
Property |
Sets and gets the Boolean that indicates if an Exception is a robot or general type. |
|
exception_obj.RobotNum |
Property |
Sets and gets the number of the robot associated with a robot Exception. |
|
exception_obj.UpdateErrorCode |
Method |
Updates a general (vague) Exception error code with a more specific error code. |