UiPath

Error handling with UIPath try-catch and retry scopes

In the development of UiPath Studio, we can make the robot run stably by implementing the processing when an error occurs.

However, many people with no programming experience are confused because they have no experience in error handling.

This article explains how to handle errors with try-catch and retry scopes and throws.

 

 Related Articles Learn the Creation Techniques f UiPath robotics creation with Udemy’s online courses that take it up a notch

This site was created by translating a blog created in Japanese into English using the DeepL translation.

Please forgive me if some of the English text is a little strange

Type of error

The types of errors are divided into application exceptions and business exceptions.

application exception

An application exception is an error caused by a technical problem, such as a system or network problem.

Some examples of application exceptions in UiPath are

  • Selector not found and cannot be clicked
  • The specified file cannot be found.
  • Converted a string variable to numeric type.
  • A variable of data table type specified a matrix that does not exist.

 

F-pen
F-pen
When an application exception occurs, the process will halt if no error handling is done.

Business Exceptions

A business exception refers to the fact that the data used in an automated process is incomplete from a business perspective.

Business exceptions in UiPath include, for example

  • Data where the product price exceeds the limit
  • Data with incorrect product codes
  • Product data that has been purchased by a department that does not have the authority to make the decision
sea otter
sea otter
Business exceptions should cause errors, stop the process, and get people to fix the data.

 

Processing according to the content of the exception occurrence (Try Catch).

The “Try Catch” activity is used to continue processing depending on the nature of the exception raised.

“Try Catch” is located in System> Activities> Statements.

Try Catch Structure

F-pen
F-pen
TryCatch is composed of three levels: (1) Try block, (2) Catches block, and (3) Finally block.
  • (1) Try block
    Places processing that may cause an exception.
  • (2) Catches block
    Performs processing according to the type of exception. It is also possible to place multiple types of exceptions.
  • (3) Finally block
    Places the final processing to be done regardless of whether or not an exception occurs.

 

sea otter
sea otter
(1) If an exception occurs in the Try block, (2) the Catches block will process the exception type, and (3) the Finally block will process it.
F-pen
F-pen
If no exception occurs in (1) Try block, (2) Catches block will be skipped and only (3) Finally block will be processed.

 

Try Catch  Setting item

Setting Location Setting item Setting Contents
Properties Common DisplayName The display name of the activity.
Misc
Private If selected, the values of variables and arguments are no longer logged at Verbose level.

 

Sample Process
It raises an exception by converting characters to integers, outputs error messages to the log with Catches, and outputs the exit message to the log with Finally.
・Try block

・Catches block

・Finally block

・Execution result

 

Raises an exception at an arbitrary time (throw)

If you want to raise an exception at an arbitrary time, use the Throw activity.

The Throw can be found in System> Activities> Statements.

 

Throw  Setting item

Setting location Setting item Setting contents
Properties Common DisplayName The display name of the activity.
Misc
Exception Exceptions thrown by the activity.
Private If selected, the values of variables and arguments are no longer logged at Verbose level.

 

Sample Process
Throw raises an error, Catches outputs an error message to the log, and Finally outputs an exit message to the log.
・Try block

– Throw properties

sea otter
sea otter
By setting “new <exception name>” in the exception part, you can generate any kind of error.

 

・Catches block

・Finally block

・Execution result

 

Exception handling according to the type of exception

List of major exceptions

例外の種類 例外の説明
System.Exception All exceptions
System.ArgumentException One of the arguments passed is invalid.
System.NullReferenceException Exception on null object reference
System.IO.IOException Exception when an I/O error occurs
System.InvalidOperationException Invalid method call exception
System.IndexOutOfRangeException When using an out-of-bounds index
SelectorNotFoundException Specified selector does not exist.
BusinessRuleException Business Exceptions

 

One of the arguments passed is invalid (ArgumentException)

ArgumentException exception will be thrown if any of the arguments passed are invalid.

 

Sample Process

  1. Reads a CSV file as a data table
  2. Specify a non-existent data table by its column name in the Try block
  3. Picks up ArgumentException exceptions in the Catches block and outputs error messages to the log
  4. Output messages to the log with Finally block

・Read CSV  Properties

 

・Cathes  block

・Finally  block

 

・data.csv  contents

・Execution result

 

Exception on null object reference (NullReferenceException)

NullReferenceException exception is thrown when a null object is referenced.

 

Sample Process

  1. Reads a CSV file as a data table
  2. Specify the number of rows in the data table that do not exist in the Try block
  3. Picks up NullReferenceException exceptions in the Catches block and logs error messages
  4. Output messages to the log with Finally block

・Read CSV  Properties

 

・Catches block

・Finally block

 

・data.csv  contents

・Execution result

 

 

Exception when an I/O error occurs (IO.IOException)

IOException exception will be raised when an I/O error occurs.

 

Sample Process

  1. Delete a text file with Try block
  2. Catches block to pick up IOException exceptions and log error messages
  3. Output messages to the log with Finally block

・Catches block

・Finally block

・[Data] folder

・Execution result

sea otter
sea otter
You are getting an IOException because the file does not exist and the deletion failed.

 

Invalid method call exception (InvalidOperationException)

Invalid method calls will result in a System.InvalidOperationException exception.

 

Sample Process

  1. New variable of type List
  2. Output the first value of list1 of List variable to the log in Try block
  3. Pick up the InvalidOperationException exception in the Catches block and output the error message to the log
  4. Output a message to the log with the Finally block

・Catches block

・Finally block

・Variables

 

・Execution result

F-pen
F-pen
The List type variable, list1, does not contain anything, so specifying the first value will result in an InvalidOperationException.

 

When using an index out of bounds (IndexOutOfRangeException)

If an out-of-bounds index is specified, System.IndexOutOfRangeException will be thrown.

 

Sample Process

  1. Assign {10,20,30} to a variable of type Array
  2. Specify a non-existent index number for the Array variable in the Try block
  3. Picks up the IndexOutOfRangeException exception in the Catches block and outputs the error message to the log
  4. Output a message to the log with the Finally block

・Catches block

・Finally block

・Variables

・Execution result

 

Specified selector does not exist (SelectorNotFoundException)

If the specified selector does not exist, SelectorNotFoundException will be thrown.

 

Sample Process

  1. Click on the search window on the Yahoo top page with the Try block
  2. Pick up SelectorNotFoundException exception in the Catches block and output the error message to the log
  3. Output message to the log with Finally block

・Catches  block

・Finally  block

・Click  Properties

・Execution result
*Run the process without opening the IE browser

 

 

Invalid cast or conversion exception (InvalidCastException)

Invalid cast or explicit conversion will result in a System.InvalidCastException.

 

Sample Process

  1. Try block to convert character a to Int type
  2. Catches block to pick up InvalidCastException exception and output error message to the log
  3. Output messages to the log with Finally block

・Catches block

・Finally block

・Execution result

 

 

Business exception handling (BusinessRuleException)

If you want to raise an error in your business, throw a BusinessRuleException.

 

Sample Process

  1. Try block to throw BusinessRuleException
  2. Catches block to pick up BusinessRuleException exceptions and output error messages to the log
  3. Output messages to the log with Finally block

・Catches block

・Finally block

・Throw  Properties

・Execution result

 

Logging when exceptions occur

Take Screenshot

If you want to identify the screen where the error occurred, you can use Take Screenshot and Save Image to save the error screen

Take Screenshot is located at UI Automation>Element>Attribute.

Save Image is located at UI Automation>Image>File.

 

Take Screenshot  Setting item

Setting location Setting item Setting contents
Properties Common DisplayName The display name of the activity.
ContinueOnError Specifies if the automation should continue even when the activity throws an error. This field only supports Boolean values (True, False).
Output Screenshot The resulted screenshot. The field supports only Image variables.
Options WaitBefore Delay time (in milliseconds) before taking the screenshot of the specified UI element. The default amount of time is 300 milliseconds.
Misc
Private If selected, the values of variables and arguments are no longer logged at Verbose level.
Target.Selector Text property used to find a particular UI element when the activity is executed. It is actually a XML fragment
Target.TimeoutMS Specifies the amount of time (in milliseconds) to wait for the activity to run before the SelectorNotFoundException error is thrown.
Target.WaitForReady Before performing the actions, wait for the target to become ready.
Target.Element Use the UiElement variable returned by another activity.
Target.ClippingRegion Defines the clipping rectangle, in pixels, relative to the UiElement, in the following directions: left, top, right, bottom.

 

Save Image  Setting item

設定場所 設定項目 設定内容
Properties Common DisplayName The display name of the activity.
ContinueOnError Specifies if the automation should continue even when the activity throws an error.
Input FileName The full file path where you want to save the image, and its name.
Image The image that you want to save to your disk.
Misc Private  If selected, the values of variables and arguments are no longer logged at Verbose level.

 

Sample Process

  1. Click on the Yahoo! top search window in the Try block of Trycatch (open the msn.com screen when the process is running, and raise SelectorNotFoundException).
  2. In the Cathes block, output the error message to the log and save the screenshot as a file with the date and time.
  3. In the Finally block, output the message to the log.

・active window

・Click  Properties

・Catches  block

・Finally block

・Take Screenshot  Properties

・Assign  Properties

 

・Variables

・Execution result

・Screenshot image file output destination

F-pen
F-pen
For more information on date and time manipulation, please see the link “Introduction to date and time manipulation in UiPath with samples“.

 

Log Message

Use Log Message to output the error level and message to the log.

The Log Message can be found in Programming > Debug.

 

Log Message  Setting item

Setting location Setting item Setting Contens
Properties Log Message The message you want to log.
Level The severity level of the message to be logged.
Common DisplayName The display name of the activity.
Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.

 

Logging Levels in UiPath

Log Level Default Logs User-Defined Logs
Off None None
Critical All messages logged with Critical level or higher. All messages logged with Critical level or higher.
Error All messages logged with Error level or higher. All messages logged with Error level or higher.
Warning All messages logged with Warning or higher.  All messages logged with Warning or higher.
Information All messages logged with Information or higher. All messages logged with Information or higher.
Trace  All messages logged with Trace level or higher. All messages logged with Trace level or higher.
Verbose All messages logged with Trace level and Workflow Tracking logs. All messages logged with Trace level.

 

Sample Process
Output log messages for each level.

・Execution result

 

Process again when an exception occurs (retry scope)

If an exception occurs during the process and you want to do the same process again, you can use the “Retry Scope” activity.

The Retry Scope is located in Workflow > Control.

 

Retry Scope  structure

F-pen
F-pen
The retry scope consists of two levels: (1) action blocks and (2) condition blocks.
  • (1) action blocks
    Place the activity to be retried.
  • (2) condition blocks.
    Place an activity that returns a Boolean value to determine if it should be retried
    (EX: Element Exists , Image Exists  ,  Text Exists)

 

retry scope  Setting item

Setting location Setting item Setting Contents
Properteis Options NumberOfRetries The number of times that the sequence is to be retried.
RetryInterval Specifies the amount of time (in seconds) between each retry.
Common DisplayName The display name of the activity.
ContinueOnError Specifies if the automation should continue even when the activity throws an error.
Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.
sea otter
sea otter
The process will be repeated until the number of times specified in “NumberOfRetries” is reached or no exception occurs and the Condition block returns True.

 

Sample Process
Repeat the process for the search window on the Yahoo top page until the click and element detection succeeds or fails five times.

・Retry Scope  Properties

F-pen
F-pen
The number of retries is set to 5, and the retry interval is set to 3 seconds.

 

・Click  Properties

sea otter
sea otter
I’ve set the timeout (milliseconds) to 100 milliseconds. If nothing is set, it will be 30000 milliseconds (30 seconds).

 

・Element Exists  Properties

F-pen
F-pen
As with the click, the timeout (milliseconds) is set to 100 milliseconds.

 

 

・Variables

・target site(Yahoo!のtop page)

・Execution result
*Display the Yahoo! top page after 9 seconds of process execution.

sea otter
sea otter
You’re out of the process in the retry scope for the third time.

 

Error handling (Rethrow) in higher-level workflows

When an exception occurs, use “Rethrow” to perform error handling in the upper-level workflow.

Rethrow is located in System> Activies> Statements.

 

Rethrow  Setting item

Setting location Setting item Setting location
Properties Common DisplayName  The display name of the activity.
Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.

 

Sample Process

  1. Calling a workflow file in the Try block of a higher-level workflow
  2. Throw BusinessRuleException in the Try block of the lower workflow
  3. Re-throwing in the Catches block of the lower-level workflow
  4. Output an error message in the Catches block of the upper workflow
  5. Output a message in the Finally block of the upper workflow

・Catches block in the upper workflow

・The Finally block of the upper workflow

・Subordinate Workflow

・Catches block for lower-level workflows

・Finally block in the lower level workflow

・Throw property of lower-level workflow

・Execution result

F-pen
F-pen
Since we are rethrowing at Catches in the lower workflow, Finally in the lower workflow will not be executed, and Cathces and Finally in the upper workflow will be executed.

 

Terminate Workflow when an exception occurs

To terminate the workflow when an exception occurs, use “Terminate Workflow”.

The “Terminate Workflow” is located in System> Activites> Statements.

 

Terminate Workflow  Setting item

Setting location Setting item Setting contents
Properties Common DisplayName  The display name of the activity.
Misc Private If selected, the values of variables and arguments are no longer logged at Verbose level.
Exception Gets or sets the exception that caused the termination of the instance.
Reason String input argument containing the reason for terminating the workflow instance.

 

Sample Process

  1. In the Try block, convert a character to an int type and raise an exception.
  2. In the Catches block, terminate the workflow after outputting an error message.

・Catches block

・Finally block

・Terminate Workflow  Properties

・Execution result popup

・Execution result

sea otter
sea otter
Since the Catches block terminates the workflow, the log output of the Finally block is not executed.

 

Continue processing when an exception occurs

If you want to continue processing when an exception occurs in an activity, set the “ContinueOnError” checkbox in the properties of the specific activity to True.

penguin-sanん
penguin-sanん
If “ContinueOnError” is blank, the default value of False will be applied.

Examples of activities that have the “ContinueOnError” property are “Click”, “Get Text”, and “Extract Structured Data”.

 

Summary

  • Exceptions are divided into application exceptions, which are caused by technical issues such as systems and networks, and business exceptions, which are caused by incomplete data from a business perspective.
  • Try-catch is divided into Try block, which arranges the processing that may cause errors, Catches block, which handles errors according to the type of exception, and Finally block, which arranges the final processing to be done.
  • If you want to raise an exception at an arbitrary time, use throw.
  • If you want to process again when an exception occurs, use the retry scope.

Back to Table of Contents

 

 Related Articles Learn the Creation Techniques f UiPath robotics creation with Udemy’s online courses that take it up a notch

 same category UiPath

 

The operator of this blog, F-penIT blog