How to Use the Try-Catch Activity in UiPath Studio

How to Use the Try-Catch Activity in UiPath Studio

Introduction

The Try-Catch in UiPath Studio is an activity for managing exceptions. It allows you to determine how to respond if an exception is thrown so that you can handle it appropriately. This helps to prevent the UiPath robot from abruptly breaking in the event of an exception, and it's considered a key element of RPA development best practices.

How to use it

Structure of the Try Catch 

The Try-Catch activity contains three main sections:  1. Try that holds the activity or set of activities that could throw an exception.

2. Catches to indicate the exception type and, optionally, holds an activity or set of activities that'll be executed when the specified exception is thrown. 

3. Finally, holds an activity or set of activities that'll always be executed. 

Try - the activity or sequence of activities performed which has a chance of throwing an error. 

try-catch-try

Catches - the activity or set of activities to be performed when an error occurs. Here you must select the exception type to look for. 

try-catch-catches

Finally - the activity or set of activities to be performed after the Try and Catches blocks are executed regardless of the result, which means it'll be executed if an error is caught or no errors occur. 

try-catch-finally

Best practices for the Try Catch activity

There's no one right way to use the Try-Catch activity, as it depends on the design of your automation or process and its structure. However, you can avoid common mistakes and follow best practices. Here are some useful tips. 1. How to throw an exception properly 

In UiPath, it's possible to throw both business and system exceptions to indicate when an error has occurred in your automation process. Here's how you can throw each type of exception. Business Exceptions: business exceptions are user-defined exceptions that you can throw in your automation process to indicate a specific error has occurred. To throw a business exception, you can use the "Throw" activity and specify the type of exception you want to throw. Example: New BusinessRuleException("You message here") 

business-rule-exception

System Exceptions: System exceptions are pre-defined exceptions that are built into the .NET framework. To throw a system exception, you can use the "Throw" activity and specify the type of exception you want to throw.  

Example: New System.IO.FileNotFoundException("file not found") 

file-not-found-exception

2. Try to avoid using nested Try-Catch  When you use nested Try-Catch activities, it makes it harder to manage your Catches. Also, it doesn’t look good in your code, making it hard for maintenance or changes on your project.

try-catch-example

3. Try to catch specific exceptions  In this way, you'll have control on what to do depending on the exception throw. 

try-catch-example-to-follow

4. Remember that the Catches will always match the most specific exception first. In the example below, when the SelectorNotfoundException is thrown, it'll match the correspondent exception in the Catches, even if the Generic Exception is set. 

try-catch-example-specific-exceptions

5. Keep in mind to distinguish between Business Exceptions and System Exceptions  Business Exceptions are related to processed data and the business logic. A Business Exception will be treated differently because it’s related to the business specifications. This is something that can be controlled and must be assessed in the project requirements gathering. 

System Exceptions are always related to the applications that the robot interacts with. A System Exception can happen anytime and can or can’t be expected. So, the robot must be prepared to handle this type of exception. This is something present on the UiPath REFramework. 

try-catch-example-business-and-system-exceptions

Common exceptions in an UiPath RPA project

Here are some of the most common exceptions you may encounter during your RPA project development. Of course, there are plenty of other exceptions, but these are the ones you're likely to face sooner or later.  System.IO.IOException - Occurs in many file I/O operations.

  • UiPath.Core.SelectorNotFoundException — The most common exception in UiPath, related to a selector that may have changed or that the robot was unable to find. 

  • System.NullReferenceException — Exception related to calling a method on a null object reference. 

  • System.ArgumentNullException — Occurs when a null argument is used in an invoked workflow. 

  • IndexOutOfRangeException - The index is outside the range of an array or collection, meaning it exceeds its capacity. 

  • TimeoutException — Occurs when an operation that was supposed to take place within a certain timeframe has expired. 

Conclusion 

The Try-Catch activity is an essential tool for managing exceptions in UiPath RPA projects. Exceptions can occur at any time and their proper handling can mean the difference for a successful project. By using the Try-Catch activity and following best practices, you can ensure a robust and stable automation process. Don't forget, proper exception handling is key to the success of your automation projects. 

Get recognized for the most creative AI-powered automation use cases in your industry. UiPath AI Center, Document Understanding and Task Mining have the spotlight. Votes will count until February 23, 2023. Participate in the challenge here.

Topics:

Studio
Avatar Placeholder Big
Lucas Pimenta

Senior RPA Developer, KCS iT