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.
Table of Contents
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.
- Open your uipath studio on your system and create a new process.
- Now search for the “Try Catch” activity and add it to your workflow.
![Best Practice for Error Handling: Try Catch UiPath Activity Try Catch UiPath Activity](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOsH_4t2JRDoMv0gCXMbxhuI1wzU9CEm2LflUCiWqtzxlcDW9EWX9BIvUZVUlxvVC5qEb72FWnenxLzZ-znVM9HbGdee5pvqS5yBhYzF5lkK0_ErWxIcYjXXhhIHa-cQ0fL_4ykNc5zOJHxqakg4nW91jyn9bLRJAHpU0LfwueePpaDU7q4mjaovSNTQ/s16000-rw/Try%20Catch%20UiPath%20Activity.webp)
Various uipath try catch exception types are:
Exceptions | Description |
---|---|
System.Exception | It includes All exceptions |
System.ArgumentException | This exception triggers one argument that is not available or not created. |
System.NullReferenceException | When the referenced object is null, this specific exception occurs. |
System.IO.IOException | This occurs when an I/O error occurs |
System.InvalidOperationException | This exception is triggered when an Invalid method call exception |
System.IndexOutOfRangeException | It occurs when accessing the values out of the index. |
SelectorNotFoundException | This type of exception occurs when UiPath selectors are not found |
BusinessRuleException | It 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.
- 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:
![Best Practice for Error Handling: Try Catch UiPath Activity Values of CSV File](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5Pq-goxxb9jNmfEaPCs8aFyjSD2-HnR_FU9mI3kb42zwYvDLrogG6xNInouPIA28YuyWn8ZOiTD3mC43TRSGZSB_oAVtO3ZDP_Cg1NiapUbZ99HfhjmFL35gkvcbLkAxeEXCQDnhEOqyLzoLTnHl-9AVVVmrIQg69t7mhz0_sBYJusJ-v6j1GrKiETw/s16000-rw/UiPath%20Try%20Catch%20Read%20CSV.webp)
- 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.
- Now drag and drop a “try-catch” activity to your workflow. Now we have to configure it.
![Best Practice for Error Handling: Try Catch UiPath Activity UiPath Try Catch Example Workflow](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijdwug2sLRBpKfhlg09WbagbQP6vfuQW575oDukAVkzHmM5efJidEfOscUsqhd9b-U2Tw7VgwKzVj2EhW6smQsVlM93IwAQHP0rt2o7olUgksG0aWt7_V50nAAkba7KroFXCQwa_QALQ08FRP1JHeAF81nrm_-IxtFAqB92D8SWI66cCZYbf--jq_-aQ/s16000-rw/Try%20Catch%20UiPath%20Workflow.webp)
- Now you have to run it and in the output section, you can see this output.
![Best Practice for Error Handling: Try Catch UiPath Activity Try Catch Output](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2T00pSrAYNV6GrzCHkiC5jFvG0XcBvCSSX2jw7qiS4Cqj8xFSFnmvPssMLp_Kx0sV03V8f8H5OXQk9P65eomh01srGzzAvz-r8Gv4HAJpmIaTBzD2WclfOW6zfekdFx3L_tXy_JeC_tXvpRJcD0JH-Z_FZqDHHW6dLVbKsaQo7qV-YHlqg1mL7Ln17w/s16000-rw/UiPath%20Try%20Catch%20Output.webp)
After executing this you can see that an “Argument error” occurred due to the undeclaration of “Data_Output”.
Properties of UiPath Try Catch Activity
Properties | Description |
---|---|
DisplayName | It gives information about the name of the activity. |
Misc | Misc 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 u0022Try Catchu0022 activity to your workflow. Then, place the activities you want to execute inside the u0022Tryu0022 block. If an exception occurs during the execution of the u0022Tryu0022 block, UiPath will move to the u0022Catchu0022 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.