Try Catch

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. For 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.


Try catch robot example

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 code)

Name of an exception

Name of a 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).