Exception handling structure that captures execution exceptions within a block of instructions and, if necessary, executes statements to field the exception.
Try
[try_statements]
[ Catchexception_object
[catch_statements]]
[ Finally
[finally_statements]]
End Try
Prerequisites
If "Break on exception code" (DataID 307) is set or if an application is started in GDE with "Break on exception" enabled, any active Try...Catch structures are ignored. These features are provided as debugging and diagnostic aids.
Parameters
try_statements
Optional statement or list of statements whose exceptions, if any, will be handled by another block of code rather than immediately resulting in the termination of thread execution.
exception_object
Exception Object, required if the Catch statement is defined. When an exception occurs during the execution of the try_statements, the exception description is automatically stored in the exception_object prior to the execution of the catch_statements. The exception_object must already have a data section allocated prior to the execution of the Catch, i.e. the New qualifier should have been previously used in a Dim statement to instantiate the Object.
catch_statements
Optional statement or list of statements that are executed if an exception occurs during the execution of the try_statements.
finally_statements
Optional statement or list of statements that are always executed at the successful completion of the try_statements or the completion of the catch_statements.
Remarks
If an exception of any type occurs when the try_statements are executed, rather than halting execution and reporting the 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.
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 to an arbitrary depth. 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.
If an exception occurs within a procedure that is invoked within a Try structure with a Catch, the execution of the procedure is immediately terminated and execution will continue at the first instruction in the catch_statements in the calling procedure. This feature allows a single Try Catch to be placed at a very high-level and capture any exceptions in any lower level routines. This case is illustrated in Example #1 below.
Alternately, if the called procedure generates an exception within a Try structure with a Catch, the catch_statements within the called routine will service the exception. However, if an exception occurs in a called procedure within a Try without a Catch but with a Finally, the finally_statements in the called routine will be executed first, then execution of the called procedure will be terminated, after which execution will continue in the catch_statements of the calling procedure. This case is illustrated in Example #2 below.
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. These special limitations are illustrated in Example #3 below.
Lastly, an Exit Try statement is provided for prematurely terminating a series of try_statements or catch_statements. When this instruction 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.
Examples
Example #1
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
Example #2
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
Try
ii = 1 / 0 ' Generates exception
Console.WriteLine("Inside Test") ' Never gets here
Finally
Console.WriteLine("Finally in Test") ' Is executed
End Try
Console.WriteLine("Test done") ' Never gets here
End Sub
Example #3
Dim exc1 As New Exception
Dim index As Integer
Robot.Attached = 1
Try
retry:
Move.Loc(loc1, profile1)
Move.WaitForEOM
Catch exc1
Controller.SystemMessage(exc1.Message)
Controller.ShowDialog("Ok,Cancel","Retry?",index)
If index = 1 Then
If Robot.Attached = 0 Then
Controller.PowerEnabled = True
Robot.Attached = 1
End If
GoTo retry ' LEGAL BRANCH
End If
GoTo bad_jump ' ILLEGAL BRANCH!!!
End Try
bad_jump:
See Also