How to create an RPA flow using Try-Catch activity
Overview
Handle exceptions with the Try-Catch activity. This activity can be used with any of the RPA Activities that might throw an exception in a scenario, and is also equipped with handling exceptions thrown by specific RPA Activities, such as OCR, Workflow, Web, etc.
To help us understand the basics of one of the many use cases this activity will help us with, we will take an example of working with Excel. Now, in a lot of scenarios, we will be using our Excel Activities. When you run an RPA flow that uses the Excel activities, if the Excel application is open, the activities won’t be executed, and you will get a 500 error. We will handle this scenario using Try Catch.
Components
- RPA Management
- RPA Studio
- Excel Desktop application
Requirements
- Before we start building the flow, you’ll need to have an Excel workbook available locally.
- Also, you’ll need to install RPA Studio on your machine and set up a connection between the RPA Studio and RPA Management, the on-premise component and multitenant component, by using the connection files from API Gateway. Check the Infor RPA Studio User Guide for the steps on how to do so.
- Create an Excel workbook available locally e.g “C:\RPA\test.xlsx”
Tutorial
Difficulty: Easy
Estimated completion time: 15 Minutes
To use Try Catch in this tutorial, we first need to understand the basics of Try Catch. Try Catch is an exception-handling block in programming. In simple words,
Try: tries a scenario that might throw an exception
Catch: catches the exception using the appropriate exception class. Based on the exception caught, the RPA flow can then be redirected to a desired outcome.
Finally: this block executes irrespective of whether an exception was caught or not.
Now lets dive into the steps to create an rpa flow that is able to catch exceptions:
1 – 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.
2 – On the Home page of RPA Studio click Create New Project and specify the following properties in the pop up window:
(a) Name: TryCatchExcel
(b) ProjectLocation: <default>
(c) Description: Try Catch with Read Range in Excel.
(d) Language: VB <selected by default>.
3 – TRY – In the use case we have selected, any Excel activity will not run if Excel is running. These activities could be anywhere in the RPA Flow.
Let us start with drag and dropping Try Catch activity in the sequence we are building.
Next look up a Read Range activity and placed it in the Try block and fill-in its properties:
- for input property Workbook Path create an Argument or Variable called path with default value “C:\RPA\test.xlsx“
- for input property Workbook Name , an create Argument or Variable called wsname of type string and default value “Sheet1”
- for input property Range create an Argument or Variable called range of type string and default value “A1:B2”
- for output property Data Table create a variable called dt of type datatable (Sytem.Data.DataTable)
Now, when this RPA Flow runs, and if Excel is running, this activity will encounter an error. Now for this activity to throw an exception at this point, be sure to uncheck the Continue on Error property in the Properties panel.
Note: Be sure to output the DataTable in a variable. We will use this variable to validate whether the Excel was read or not later.
If Excel is running, an exception will be thrown which will be caught in the Catch block.
4 – CATCH – There can be different types of exceptions and they are caught using appropriate Exception classes. In this case we will simply use System.Exception to catch ours.
Along with catching the exception, we can add an activity or a sequence to handle that exception.
Here, we will add a Sequence activity and inside of it a Message Box activity and Delay activity . The message will display the Exception we have encountered and also the further instructions to handle it. “exception.message” expression displays the exception encountered.
For that configure the Message Box:
- for Input Text property provide the following text: exception.message + “RPA cannot access Excel since it is open. Close Excel and click on OK to proceed.” We ask the user to close Excel before proceeding and then click on OK on the message box. We have used a Delay to allow the user to complete these actions
For the Delay activity configure
- Duration property 00:00:05 as it follows the format HH:MM:SS
5 – Finally – Finally block executes irrespective of whether an exception was caught or not. We will take advantage of this property to validate whether our Excel was running in the background or not.
We will place an IF block inside the Finally and the condition value of “dt is Nothing” but specifying it inside of the Condition property. This is a check to see whether the output variable of “Read Range” is empty or not. In case Excel was running, “Read Range” will not work and nothing will be stored in dt. If there is an output in dt, this means Excel was closed and the activity worked.
If Excel was open at first and then had to be closed, then it means we would need to read it again using Read Range. For that in the Then block inside of the IF activity add the Sequence and inside of it add Read Range and configure it identically as the previous Read Range activity. Next after the Read Range add Message Box, and inside Input Text property specify: dt.tostring
Note: If Excel was not running, then nothing needs to be done.
6 – Before you run the flow make sure to open the excel file which you are checking. Click on “Run” to run you RPA Flow. When you get the fist Message Box close the Excel file so the flow continues to run.
This RPA Flow can be used in any use case where we are working with Excel. This implementation of TRY CATCH covers its basic functionality.
Similarly, TRY CATCH can be used with any of the RPA Activities that might throw an exception in a scenario. and is also equipped with handling exceptions thrown by specific RPA Activities, such as OCR, Workflow, Web, etc.