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 mentioned directory.
Note: Before trying to understand and work with Throw and Rethrow, it is important to understand the concept of Exception Handling with Try Catch. To learn more about Try Catch, find an example here – How to create an RPA flow using Try-Catch activity.
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 Catches block and the Finally block.
5. In the Try block we will add a Sequence that contains the Read Text File, Message Box and an If Else activities, and configure it as followed:
- Read Text File – This activity will reference the text file that we created in the pre-requisites section. In the:
- for Source Filepath property we will give the path of the file that we created
- let’s create the Argument called file path of type string with default value “C:\RPA\Exception_example.txt“.
- for the Output properties Text which value will contain the text that was read let’s create the variable
- called op of type string without default value
- for Source Filepath property we will give the path of the file that we created
- 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 file in the referenced path.
8. Now before going to the next steps, we need to 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.n 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 and configured as following:
- for Condition, let’s create the variable
- called exceptionCondition of type Boolean
10. Then if the exceptionCondition is true then we need to add the Throw activity inside the Then block of it which will then trigger throw an exception that we want it to throw. Let’s configure Throw activity in a following way:
- For the Exception property specify: new System.IO.IOException(“Input Output Exception“)
11. For Else specify the Message Box, for which the input Textproperty contains: “False – else statement” simply to show that the control is in else.
12. For this example, I have set the exceptionCondition as true so that we always trigger the Then block and Throw an exception.
13. 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. For our Catch Block select System.IO.IOException and then you start adding the activities.
14. First add the Sequence as otherwise you would be able to add a single activity within this block. Next just for troubleshooting reason add Message Box to show the expectation and specify the text as followed: <>
15. Our Catches 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.