Have you ever encountered an error or exception while running a UiPath workflow? If so, you are aware of how annoying it may be when unknown problems cause your automated process to be disrupted. To solve these issues UiPath provides a Try Catch UiPath activity to help you handle these errors.

In this article, we will be going through this activity to know more and to show how you can implement it on your UiPath Studio.

What is Try Catch UiPath Activity?

UiPath Try Catch activity is one of the features that uipath provides. This activity is used to handle or manage errors in the UiPath studio.

It helps you to choose how to react or what to do when an exception occurred during your process running so that with the help you are able to resolve it easily without affecting your process. It also helps to prevent the UiPath Bots or Robots from Stopping or Breaking in the case an exception occurs. The package used in this activity is:

System.Activities.Statements.TryCatch

This activity is divided into 3 parts:

  • Try: contains the action that could cause an exception.
  • Catches: indicates the exception type and, if desired, performs an activity to notify the user of the discovered exception;
  • Finally: contains a procedure that ought to be carried out only in the absence of errors or after they have previously been detected.

The “try block” is used to test whether the activities inside it work properly. If an activity causes an error within the try block, it is caught by the “catch block”. The “finally block” always executes, even if an error occurs in the catch block.

How to Use Try Catch in UiPath With Example

Now you have a basic idea of try-catch, now will be going through how you can use this in your process with help of examples.

  1. Open your uipath studio on your system and create a new process.
  2. Now search for the “Try Catch” activity and add it to your workflow.
Try Catch UiPath Activity
Try Catch UiPath Activity

Various uipath try catch exception types are:

ExceptionsDescription
System.ExceptionIt includes All exceptions
System.ArgumentExceptionThis exception triggers one argument that is not available or not created.
System.NullReferenceExceptionWhen the referenced object is null, this specific exception occurs.
System.IO.IOExceptionThis occurs when an I/O error occurs
System.InvalidOperationExceptionThis exception is triggered when an Invalid method call exception
System.IndexOutOfRangeExceptionIt occurs when accessing the values out of the index.
SelectorNotFoundExceptionThis type of exception occurs when UiPath selectors are not found
BusinessRuleExceptionIt occurs in Business exceptions.

The above table shows UiPath Try catch all exceptions types.

Example for Using Try Catch UiPath Activity:

Based on the above we will provide an example for anyone with the exception mentioned above. To demonstrate an example here we are using “System.ArgumentException“.

This exception occurs when an argument that is trying to access is not available or not created. To practice, this exception follows the below steps.

  1. Drag and drop the “Use CSV” Activity to your workflow. Add the file path of your CSV file and set the output as “Data_Output“. The values in the CSV files are:
Values of CSV File
Values of CSV File
  1. We have added the output argument of the above CSV file as “Data_Output” but have not declared it. So there is a chance to occur an error while execution.
  2. Now drag and drop a “try-catch” activity to your workflow. Now we have to configure it.
UiPath Try Catch Example Workflow
Try Catch Loop Uipath
  1. Now you have to run it and in the output section, you can see this output.
Try Catch Output
UiPath Try Catch Throw Exception

After executing this you can see that an “Argument error” occurred due to the undeclaration of “Data_Output”.

Properties of UiPath Try Catch Activity

PropertiesDescription
DisplayName It gives information about the name of the activity.
MiscMisc provides private options.

Best practices for UiPathTry Catch activity

The layout and workflow of your automation process will determine the best approach to employ the Try-Catch activity. You may, however, prevent frequent errors by adhering to recommended practices. Here are some best practices for using Try catch:

1. How to throw an exception in UiPath

Both business & system exceptions can be thrown in UiPath to signal when an issue has come across in your RPA process.

Eg: New BusinessRuleException(“Enter You message here”) 

Eg: New System.IO.FileNotFoundException(“file not found Error”)

2. Try to avoid nested Try Catch uipath

It is more difficult to manage your try-catch activity when you implement nested Try-Catch activities. Also, it makes it difficult to maintain or make modifications to your project’s code, which is not acceptable.

3. Use Try Catch for Specific Exceptions

It is better to use specific exceptions such as System.ArgumentException, SelectorNotFoundException and etc. By doing so, you’ll be able to decide what to do based on the particular exception. It helps you to throw exceptions according to your needs.

4. Most Specific Exception is Caught First

Keep in mind that the most particular exception will always be matched first by the Catches.

Conclusion

In UiPath Studio, It is the best way for handling exceptions. In UiPath Studio, Exceptions can happen at any time while running the process, and the important part is how they are properly handled. As it can help your process to be successful.

By using this activity, you may ensure a safe and consistent automated process. Don’t forget that efficient exception handling is essential for the success of any automation Process.

Also check, UiPath

Faq on UiPath Try Catch Exception Handling

How to use try catch in uipath
To use try catch in UiPath, Drag and drop the “Try Catch” activity to your workflow. Then, place the activities you want to execute inside the “Try” block. If an exception occurs during the execution of the “Try” block, UiPath will move to the “Catch” block and execute the actions you have specified to handle the error
How to remove try catch in uipath
To remove try catch uipath h, you have to right-click on the activity and select delete.