How to Create an RPA flow to understand Throw and Rethrow
Overview
This tutorial will show you how to create a basic flow which uses the Throw and Rethrow activities, to understand how to work with Exceptions using the Try Catch, Throw and Rethrow.
Let’s create a flow which will allow bot to read the files and check if they contain any text. The exception scenario here could be that there was no file found in the said directory.
Components
- RPA Management
- RPA Studio
Requirements
- Before you can start building the flow, you’ll need to install the latest version of RPA Studio on your machine and set up a connection between the RPA Studio and RPA Management. Check the Infor RPA Studio User Guide for the steps on how to do it.
- Before we start building the flow, let’s first create a few artifacts needed to set up an RPA flow:
- Create one folder called RPA.
- Within the created Folder (C:\RPA) create a .txt files e.g. “C:\RPA\Exception_example.txt”.
Tutorial
Difficulty: Easy
Estimated completion time: 10 Minutes
- Open RPA Studio on your machine. If you are not signed in already into the Tenant you would be asked to sign in before proceeding to RPA flow designer screens.
- On the Home page of RPA Studio click Create New Project and specify the following properties in the pop up window:
- Name: ThrowRethrow
- ProjectLocation: <default>
- Description: Throw and Rethrow
- Language: VB <selected by default>
- We will start the flow adding a Sequence to our canvas, and the Sequence would contain the Try Catch block.
4. Inside the Try Catch block we have three sections, the Try block, the Catch block and the Finally block.
5. In the Try block we have sequence that contains the Read Text File, Message box and an If Else activities. First, we will setup this sequence by adding a Read Text File, Message box and an If Else, in that order.
- Read Text File – This activity will reference the text file that we created in the pre-requisites section.
- In the “Source Filepath” property we will give the path of the file that we created “C:\RPA\Exception_example.txt”.
- In the Output properties, we will add the string variable “op” that would contain the text that was read. To create a variable, click on Variables at the bottom of the Studio designer window and give a name, datatype to your variable.
- Message Box – We are using this activity to display the text that was read. In the Input Text property will give the “op” variable that contains the texts.
6. Now before proceeding we have to understand what we are trying to achieve here. The Try Catch block is where our exceptions are handled, so the Throw or Rethrow activities usually go hand in hand.
7. The Try block contains the activities or sequence that could possibly produce and exception. Here the activity Read Text File and a Message Box to display the contents are added. At this stage, an exception would be produced when the Read Text File does not find a find in the referenced path.
8. Before going to the next steps, let us understand the difference between Throw and Rethrow.
- Throw – Simulates an exception. In other words, this allows you to manually produce an exception that you want. It can be used anywhere in your flow where an exception is appropriate.
- Rethrow – Rethrow works in conjunction with the Catch block of a Try Catch activity and it throws an exception that is caught in the Catch block.
9. To simulate an exception, we have added an If block with exceptionCondition set as True. This will then trigger the Throw activity which will then throw an exception that we want it to throw.
10. Now if no exception is thrown, the workflow will not go to the Catch block, but in case the File is not found, then the Catch block is triggered. Our catch block would contain the Rethrow activity, which would just Throw back the Exception that was caught and terminate the workflow. This exception could be seen in the Error window.
10.The Finally block and other activities are simple indicators of where the control is in the sequence.
Throw and Rethrow activities can be used in any part of the flow that potentially might not be successfully completed. The implementation of exception handling in case of potential errors brings visibility into how to fix the causing error.