The Global Exception Handler is a type of workflow designed to determine the project’s behavior when encountering an execution error. Only one Global Exception Handler can be set per automation project.
By default, the template retries the exception 3 times before aborting the execution. This default behavior can be changed from inside the template.
The Global Exception Handler is not available for library projects, only processes.
The Global Exception Handler has two arguments, that should not be removed.
The first argument is
errorInfo with the In direction and it stores information about the error that was thrown and the workflow that failed. The level of the error to be logged can be set in the Log Message activity.
ActivityInfo property for
errorInfo to get the name of the activity which threw the exception and view it in the Output panel.
The second argument,
result has the Out direction and it is used for determining the next behavior of the process when it encounters an error. The following values can be assigned to the
- Continue - The exception is re-thrown.
- Ignore - The exception is ignored, and the execution continues from the next activity.
- Retry - The activity which threw the exception is retried. Use the
errorInfoto count the number of times the activity is retried.
- Abort - The execution stops and throws the exception after running the current Global Exception Handler.
Any workflow may be flagged as a Global Exception Handler in Studio, except for library projects.
When an exception is detected during debugging, the activity which faulted is highlighted, the execution is paused, and the exception's type and details are mentioned in the Locals and Call Stack panels.
Debugging actions like Continue, Stop, Retry, Ignore, Restart and Slow Step are available in the ribbon. Ignore is used for continuing the execution from the next activity.
The Retry button retries to execute the current activity, without the Global Exception Handler stepping in. The Continue action runs the Global Exception Handler, taking into consideration the previously chosen values for the
result argument, either Continue, Ignore, Retry or Abort.
Variables defined in the workflow can be viewed in the
Variables property of the
When using the Global Exception Handler with a project that includes a Try Catch, make sure to group activities into a Sequence inside the Try container. Otherwise, the Global Exception Handler does not execute.
In the case of nested activities, the Global Exception Handler executes for each activity in the call stack. However, it does not execute for activities directly encapsulated in a Try Catch, unless they're contained in an activity.
The following example showcases the project’s behavior when an exception is thrown during execution.
The automation project is set to click inside an empty and saved Notepad document, type in some text and then close the application, but not before clicking the save button to save it.
To control the workflow’s behavior in case of an error, the Global Exception Handler retries the activity three times and then aborts with an error message.
- Create a Blank Process by following the steps in the Creating a Basic Process page.
- Open Notepad and save a document on your machine.
- From the Activities panel, drag a Type Into activity to the Designer panel. The activity is automatically added to a Sequence.
- In the Type Into activity, click on Indicate on screen. Studio minimizes and lets you click inside the Notepad window to create the selector. In the text field, type in the desired text between quotation marks.
- From the Activities panel, add a Send Hotkey activity to the workflow. Indicate the Notepad window, and select Ctrl from the activity, type in s in the Key field. This saves the Notepad document after the text was typed in.
- From the Activities panel, add a Close Application activity to the workflow. Indicate the Notepad window using Indicate on screen. The resulted workflow should look like this:
- In the Design tab part of the Ribbon, select New > Global Handler. The New Global Handler window opens. Type in a Name for the handler and save it in the project path. Click Create, a Global Exception Handler is added to the automation project.
- In the Variables tab, create an
retryCount, set its scope to the entire Global Exception Handler, leave the Default value field empty. The variable stores the number of retries and it increases the number by
1each time an error is encountered.
- Create a String variable named
failedActivityand set its scope to the entire workflow. This variables stores the name of the activity which faulted and reveals it in the Output panel.
- From the Activities panel, add an Assign activity below the Log Error in the Global Handler sequence. Add the
retryCountto the activity and assign it the
errorInfo.RetryCountexpression, which counts the number of times the exception is thrown.
- Add another Assign activity in which assign the
errorInfo.ActivityInfo.Nameexpression to the
failedActivityvariable. At this point, the workflow should look like this:
- Add an If activity to the workflow. Set the Condition to
retryCount < 3to limit the number of retries to 3.
- In the Assign activity part of the Then category, assign the
resultvariable to have the Global Exception Handler retry the activity which gave the exception.
- In the Else category, assign the
resultto have the Global Exception Handler abort the execution when the condition is met. The If activity should look like this:
When the Global Exception Handler encounters an exception, it logs the name of the activity which faulted and starts retrying the activity three times. If it encounters the same exception each time and the number of retries reaches 3, the execution is aborted at the level of the activity which threw the exception.
If during one of the retry, an exception isn’t encountered, the execution of the workflow continues and the Global Exception Handler doesn’t step in.
Updated 2 months ago