Exception Handling in UiPath

An unwanted unexpected event that disturbs the normal flow of the program is called Exception.

For example, consider that you want to go shopping on Sunday. But when you are prepared and traveled to the mall but the shop you are looking for is shut for the day. Here you might not have expected the shop is shut, but currently, it is shut. Your expectation did not work out, and this is an exception in day to day life.

Similarly, in UiPath there is a situation where you might see this kind of exceptions, for example, you want to read the file contents but the file you are trying to read might not be present at the given location.

If you thinking like I have verified the file then only I have created the flow, then think about executing the same program in different computer, in such situation will you able to say that file is present ?. no, it is not possible and due to this reason, we have to make a flow where we perform some task when the file is not present and this alternate flow is known as exception handling.

Exception handling :

Performing an alternate flow or performing a set of a task when an exception arises known as Exception handling.

Need for Exception handling:

Whenever an exception occurs most programming languages and tools will crash the execution of the current task. This might cause severe problems when you are dealing with sensitive data like financial transactions.

So we have to avoid the crash either by providing alternate ways of execution or stop the program gracefully with clearing the sensitive data.

Exception handling in Uipath

Exception Handling mainly deals with handling errors with respect to various activities in UiPath. The Error Handling activity offers four options:

  • Rethrow : Rethrowing an exception If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression ( throw without assignment_expression) causes the originally thrown object to be rethrown.
  • Terminate workflow : Terminate workflow is used to terminate the workflow the moment the task encounters an exception.
  • Throw activity : Throw activity is used when you want to throw an exception.
  • Try Catch activity : Try Catch activity is used when you want to test something and handle the exception accordingly. So, whatever you want to test you can put it under the try section, and then if any exception occurs, then it can be handled using the catch section.
    Apart from the try-catch, we also have a Finally section that performs the activities irrespective of whether an exception occurs or not.

Exception Handling in UiPath

Following example demonstrate the Exception handling

Create a new process called Exception_Handling_Example

exception-handling-process-rpa-uipath

Once the Exception_Handling_Example process opened in the UiPath Studio, add Open Browser activity inside the sequence and then enter the google URL(www.google.com) as shown below.
add-open-browser-on-exception-handling-rpa-uipath

Next, add Type Into activity inside the DO Container of the sequence and then click on Indicate element inside browser and indicate the google search box on the browser(Make sure that the browser has opened). Next, enter the text you want to search on the Google search box.
add-search-text-into-type-into-rpa-uipath

Click on the three horizontal lines on the Type into activity and select Edit Selector.

edit-selector-on-type-into-rpa-uipath

Edit the name in the Edit Selector Wizard( I am changing name='q' to name='z') and then click on the ok button. The name='q' has automatically generated from the selector if we change the name from q to z then we will get an exception.
name-q-rpa-uipath
changingname-from-q-to-z-rpa-uipath

By creating an error scenario, we will understand how the exception happens and then how to handle this exception. Save and run the sequence, You will get an InvalidUiElementException.

invalid-element-exceptio-rpa-uipath

These are very realistic scenarios in our production, we have to handle this kind of run-time exceptions happened in our production. These exceptions can be handled with the help of Try Catch Block.

Add Try Catch activity inside the sequence and move the Type Into activity inside the Try block. And then select the exception type which you are facing. If the exception type which you are facing is not present in the list then click on the Browser for Types... and search for it.
click-on-brwoser-type-rpa-uipath

I am clicking on Browser for Types... and search for InvalidUiElementException and then click on ok
invalid-ui-element-exception-rpa-uipath

  • Add Log Message activity inside the Exception block
  • Set Log Level to Error
  • And then enter the Message as Not Found Element as shown below.
    log-message-inside-exception-block-rpa-uipath

So, the error message will be displayed on the output screen when you run the sequence.
not-found-message-rpa-uipath

So, whenever an error occurs in the Try block it will jump to the catch block and instead of stopping the execution It will display a Not Found Element message on the Output pane.

The Finally block is the place where you want to execute something even if the error occurs or even if the error does not occur. Now, add Close tab activity inside the Finally block meaning that if the error occurs or not occurs the tab will be closed.
finally-block-rpa-uipath

Learn how to resolve some of the common mistakes

When you do not name the activities : It is often observed that all of us who work on UiPath, just drag and drop the activities, but nobody actually makes an effort to name the activities. Due to this, nobody understands the source of errors.

Solution: The simplest solution is to use the name of the activities. Once you name the activity, you will not get an error that Main has thrown an exception, but you would see that the name of activity has thrown an exception.

When some other application is open in front of the main application: It may happen sometimes that you are working on the N number of applications on your system. But, when you execute the project you forget to make sure that the main application in which the automation has to be done is open, and no other window/application is open in front of that.

Example: If I want to scrape data from a website and if I have Notepad++ open in front of that, then UiPath will not be able to perform the automation.

Solution: To avoid such kind of errors, you have the following options such as:

  • Make the main window/application visible, and close all other windows.
  • If you have any click actions, then change the Input Method, and choose to Simulate Click.
  • You can make sure that the right selector values are present so that UiPath is directed to the right window and performs the automation

When you have timing and synchronization issues: It may happen sometimes that you are dependent on some other application to perform some task, and that application may take time to load. In such situations, if you do not mention the time to wait, then UiPath assumes that the application is not working/opening and throws an exception.

Solution: To handle such errors, mention the time to wait in the Timeout MS parameter. By default, the value would be 30. Apart from that if you want to handle synchronization issues, then you have the following 3 options:

  • Element Exists Activity – This activity returns TRUE/FALSE based on the existence of a specific element. So, you can use this if you just want to make sure whether an element is present or not.
  • Find Element Activity – This activity will stop the automation until an element is found
  • Wait Element Vanish Activity – This activity waits for an element to disappear before continuing.

When your browser crashes : Sometimes, when you work with the browser, it may happen that browser crashes. In such situations, UiPath will not understand the dependency and will throw an Exception that it cannot load/open.

Solution: To solve such kind of errors, you can use a Try-Catch block. So, you can put the browser part in the Try section, and then either put a Message box or any other activity in the catch section. After this is done, you can mention the necessary activities, in the Finally block. So these activities would be executed after the Try-Catch block.

When you do not choose the right activity : Sometimes it may happen that you may choose the wrong activity. For example, if you search for For each activity, then you will see that there are three for each activity that can be chosen.

Solution: Choose the right activity, according to your need. To do that, you need to understand the functionality of each and every activity. For example, if you have to perform some action for each row in the data table, then choose the For-Each Row activity. Else, if you want to perform some action for each item, then choose the For-Each activity.