Try Catch

Follow

・Overview
Explanations about Try Catch on Design Studio and Device Automation

Design Studio
When Try Next Alternative error handling is used with an explicit At reference to a target Try step, the step is identified by its name. Most often, the fine distinction between the target step and its name is not important, but it can be exploited to provide exception handling functionality similar to the try-catch constructs in Java or C#.

In those programming languages, a section of code between "try" and "catch" has special error handling. If a specific error is signaled within this section (by "throwing" a named "exception"), the piece of code following the similarly named "catch" is executed. Try-catch constructs can be nested, and a named "exception" is always handled by the innermost enclosing "catch" with a matching name. 

Example:

try {
   ... code ...
   try {
     ... inner code ...
     throw new E(); // caught by innermost "catch"
   }
   catch (E e) {
     ... inner handling code ...
   }
   ... more code ...
   throw new E(); // caught by outermost "catch"
}
catch (E e) {
   ... outer handling code ...
}

In robots, something similar can be done with Try steps. Remember that an "At" reference to a Try step with a given name always means the nearest prior step with that name (along the current execution path). It is permitted to use the same name for several Try steps, even on the same execution path. Thus each try-catch construct is modeled with a Try step having the same name as the "exception". The Try step has two branches: one for the code part of the "try" construct, and one for the code part of the "catch" construct.

TryCatchRobot.png

 

The correspondence between the Java/C# syntax and the Design Studio terms is described in the table.

Java / C# Syntax What to use in Design Studio
try { …code…} The first branch of a Try step (the steps correspond to the code)
Name of an exception Name of the Try step
throw new E()

 

(within the code of a try)

Handling an error with "Try Next Alternative at E"
catch E { …code…} The second branch of a Try step named "E" (the steps correspond to code)

Thus, the core idea is: When Try steps are used for error handling, name the Try steps after the error situations they handle. The advantages are:
• The naming helps make the purpose of each Try step clear.
• When errors are handled on a general level (with a Try step more to the left in the robot), it is still easy to do specialized handling in some cases (with the aid of a second Try step with the same name).

 


【Device Automation】
The Try-catch step of DA defines a method to explicitly catch an exception that occurred when the action is executed, and process it.
The properties of the Try-catch step are built around the 3 blocks below.

■Try branch
Specifies the performed action.

When an exception occurs resulting from the execution of an action, the exception is thrown to the Catch branch.
If the action is normally executed, the Try-Catch step ends after the execution of the action specified in the Finally branch.

■Catch branch
Specifies an exception that risks to be thrown when executing the action of the Try branch.

The Catch branch is built around a list of at least one exception and the action to execute in case this/these exception(s) is/are thrown.
The exception is either explicitly thrown by a Throw step or thrown if an error occurs during the execution of another step.

If the action specified in the Catch branch is correctly executed, the Try-Catch step ends after the execution of the action specified in the Finally branch.
If a thrown exception is not listed or if the Catch branch also ends because of an exception, the Try-Catch step itself will be treated as if it threw an exception, and it will be processed with the same method used when another step throws an exception.

■Finally branch
c7d0e058464e159f874fb49c905d367c.pngSpecifies the action to perform immediately before the Try-Catch step ends.

The action specified in Finally has no relation with the execution result of Try and Catch, and is usually executed until the end. If an exception occurs in the Try branch while the Finally step is blank (not set), and, if an exception is not listed in the Catch branch, or if the Catch branch also ended because of an exception, the Try-Catch step itself will be treated as if it threw an exception and processed with the same method used when another step throws an exception.

Even if an exception occurs in the action of the Finally branch, the Try-Catch step itself will be handled as if it threw an exception.

<Use example of an exception handling by Try-Catch>

(Figure1.DA_Try-Catch Explanations)

An exception thrown by the Catch branch and the Finally branch is not processed in the Catch branch of the same Try-Catch step.

An exception that has not been processed by a Try-Catch step will be set as "not handled" in the workflow.
In that case, both the workflow and the DA step execution stop, and the error is processed as specified in tab "Error Handling" or the DA step.

4 out of 5 found this helpful

Comments

0 comments

Please sign in to leave a comment.