Skip to main content

How to use the Try - Catch Structure?

· 4 min read
Burcu Evren

When an error occurs during the operation of the flow, it is managed using both the Catch node and the Try Catch structure. Errors are an inherent aspect of many processes and can vary from simple typos to intricate issues that demand hours of debugging to rectify.

An example of the error-handling structure is provided below. This example functions by verifying the existence of a specified file path. If the file does not exist within the folder directory, it is checked three times before the flow is halted. However, if the file does exist within the folder directory, it is read, and the flow is terminated

How to Setup?

To begin, Login to your Robomotion workspace. If you haven't registered yet, you can create your workspace from the provided link.

Node Overview

  • Inject: This node initializes the flow.
  • Function: In this node, variables, objects, etc., to be used in the flow are defined.
  • Path Exists: This node checks if the given path exists. It is of boolean type, and its output is either true or false.
  • Read File: This node is used to read a specific text file.
  • Catch: It is used to catch an error if one occurs during the execution of the flow.
  • Switch: This node is used for routing the flow based on specific conditions.
  • Goto: To ensure flow continuity, this node establishes connections and routing between nodes to guarantee process repetition when an error is encountered. When the flow reaches this node, it continues from wherever it was redirected.
  • Label: This node holds the redirection point for the 'Goto' node, allowing the flow to continue from its location.
  • Stop: This node is used to halt the flow.

Flow Steps

  1. Access to the workspace is granted through the instructions provided in the "How to Set Up" section.
  2. The 'Inject' node is utilized to initiate the flow.
  3. Inside the 'Prepare' function node, the variable 'msg.filePath' is defined. This variable will serve as the input for the 'Path Exists' node, and the file path to be checked for existence should be specified.
msg.filePath = "C:\\Robomotion\\Templates\\myText.txt"; // The directory of the folder to be checked for files should be given
return msg;
  1. The 'msg.try_count' variable is defined within the 'Set Try Count' function node. This variable stores the initial count of how many times the try-catch structure will be invoked when an error occurs in the flow.
msg.try_count = 0;
return msg;
  1. The 'Path Exists' node verifies whether files exist in the specified path. This node is of boolean type, and its output can be either true or false.

  2. The 'Switch' node controls the output of 'Path Exist' node.

Switch Node Options

If the output is true, it signifies that the file exists. In such cases, the robot reads the file using the 'Read File' node, and then the flow comes to a halt. This represents the error-free scenario, and the Try Catch structure remains inactive. However, if the output is false, the flow continues with the 'throw' function node.

  1. The robot triggers an error during the 'throw' function node phase because it is unable to locate the file in the designated path.
throw "The file you defined in msg.sourcePath was not found."
return msg;
  1. The 'Catch' node contains the function node that encountered an error.

  2. In the 'Plus Try Count' node, the previously defined 'msg.try_count' index is incremented by 1.

msg.try_count += 1;
return msg;
  1. The 'Switch' node examines the value of this index. If it is less than 3, the flow is rerouted to the 'Path' using the 'Go To Path' node. The 'Path Exists' node verifies whether the file exists. In the 'Go To' node, select the appropriate 'Label' node from the options section.

Goto Node Options

If the index is greater than 3, the flow is terminated. This means that the file has been checked three times and has not been found. The Try Catch structure also operates based on the value of the index.

The index value is optional and can be modified as needed.

Switch Node Options Try Catch Structure Flow

Here you can access the complete flow by clicking on the following link: