Handle Error

Handles an exception that might occur during the script runtime advancing to the next command or triggering a subroutine.

Command availability: IBM RPA SaaS and IBM RPA on premises

Description

Exceptions are unexpected event that can cause the bot to raise errors when it runs in a production environment or in debug mode. For more information on how to handle exceptions, see Exception handling. You can also see Debugging scripts and Start options when you handle exceptions in debug mode.

Script syntax

IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

onError [--executenext(Boolean)] --label(String)

Input parameter

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
Execute Next executenext Optional Boolean Enable to run the next command if an error occurs during the script runtime.
Subroutine label Required when the Execute Next parameter is False Text Subroutine to trigger if an error occurs during the script runtime.

Example

Example 1

When an error occurs in your script, it completely stops the runtime and prevents your bot from completing other tasks. In most situations, you can simply ignore some exceptions that are not critical to your automation and try to keep the bot running until completion. In the following example, the command ignores an error that happens when you try to divide a number by zero, and the bot runs all other commands normally:

defVar --name firstValue --type Numeric
defVar --name secondValue --type Numeric
defVar --name result --type Numeric
// Handle an expected exception in the script. In this case, it advances to the next command.
onError --executenext
// Assigns values to variables to run a mathematical expression.
setVars --assignments "${firstValue}=2,${secondValue}=0"
// Calculate a mathematical expression based on a value provided before.
evaluate --expression "${firstValue}/${secondValue}" result=value
// Logs the operation's result.
logMessage --message "The operation\'s result is: ${result}" --type "Info"

Example 2

However, sometimes you want to handle errors with specific actions, following a subroutine of commands that run after the error happens. In this case, you might want the user to know what error happened during runtime, and to recover from the error, you can also assign a default value to the result variable in the script.

The following example uses a subroutine to recover from the error. If you run the script in IBM RPA Studio debug mode, you still get an error message box, but you can continue running the script after the error happens.

defVar --name firstValue --type Numeric
defVar --name secondValue --type Numeric
defVar --name result --type Numeric
// Handle an expected exception in the script. In this case, it advances to the next command.
onError --label evaluateException
// Assigns values to variables to run a mathematical expression.
setVars --assignments "${firstValue}=2,${secondValue}=0"
//Calculate a mathematical expression based on a value provided before.
evaluate --expression "${firstValue}/${secondValue}" result=value
//Logs the operation's result.
logMessage --message "The operation\'s result is: ${result}" --type "Info"
beginSub --name evaluateException
// Outputs a message indicating that the division went wrong.
	logMessage --message "Something went wrong: ${rpa:errorMessage}" --type "Info"
// Assigns 0 to the result variable	
  setVar --name "${result}" --value 0
// Recovers the script runtime, running the next command.
	recover
endSub