Exceptions handling

Principles of exceptions handling

Introduction

While creating workflows, a developer should foresee various scenarios and be able to handle them. For example, if you want to move a file between folders, there is a possibility that the file doesn't exist. Such scenario (depending on the business-process nature, of course) can be foreseen usually. Therefore, it can be processed accordingly (for instance, by checking if the file exists before copying).

However, there are exceptional situations. Sometimes they can't be predicted, sometimes they can, but it is pretty time-consuming to handle each of them. For example, when automating a process through the application's interface, it is possible that a certain element may take too much time to appear on the screen. In this case an exception may occur when interacting with element (which haven't appeared yet). Sometimes this can be processed separately. But if a workflow consists of numerous consecutive clicks or keyboard inputs then this is pretty time-consuming to handle each step let alone that the workflow may become very hard to read. In that case a common solution is to put these activities to a subprogram and handle the possible exception on a level of subprogram.

Handling mechanism

Each activity in the 'ElectroNeek Studio' contains the 'Error' port. If no branch comes out of this port, then any error in this activity will lead the workflow to stop.

If the 'Error' port has a branch coming out of it then the workflow will perform the steps on the branch and won't stop with an error message.

In the example above in case robot won't be able to find the first element it will click on the second.

Example without exception handling

Suppose we want to input some text to the notepad application in Windows.

  1. Open the notepad using any of the available methods, for example, by pressing 'win + r' - typing 'notepad' - pressing 'enter'.

  2. Return to 'Studio' and add the 'Input from keyboard' activity of the 'Interface elements' group to the canvas.

  3. In the parameters section of the added activity, expand the Interface element dropdown menu. Then press 'Pick New Element' button.

  4. The 'Element Picker' application appears which allows a user to specify the field where the desired text will be typed. Hover on the edit field of the notepad, press 'Ctrl', choose attributes according to the screenshot below, press 'Test' and make sure there is only one interface element found.

  5. Press 'Ok' in the 'Element Picker' and return to the development environment.

  6. Enter any text to the 'Value' field, for example, Exceptions handling.

  7. Run the robot.

Robot will successfully complete the task if everything has been done correctly.

If you close the notepad before starting the robot an exception will occur (the example is shown below). The robot will stop. If there were any activities following, they wouldn't be performed.

Example with exception handling

Now we will handle the exception. Firstly, we would like to know which error has occurred. Secondly, we would like to print a little bit more 'friendly' message for the user.

  1. Add the 'Assign value to variable' activity and connect it with the 'Error' output of the 'Input from keyboard' activity.

  2. Make sure that the parameters of the added activity appeared on the right panel.

  3. Type error in the 'Variable name' field and then choose the 'Save the previous step result' option (thus an information about the error will be saved to the variable).

  4. Follow the previously added activity with another 'Assign value to variable' activity. In the variable name field we'll type user_message and in the value field we'll typeAn error occurred while working with the notepad.

  5. Run the robot.

Now we can see that the workflow is successfully finished and we have not received any red error messages. We also see that in the right variables panel, two variables are shown: one with the error message and one with the text for a business-user.

Conditional exception handling

Let's improve previous example and add an if..then activity to the workflow. This way we can check for particular error and eliminate the cause of this error.

Our error has occured because Robot couldn't find an element of Notepad interface:

Selectors: {"count":0,"treeCounts":[0,0],"severalFound":false,"noneFound":true,"element":null}

In the if..then activity we can add a followig condition to check if the error was the kind that we're looking for:

error.includes('Selectors: {"count":0,"treeCounts":[0,0],"severalFound":false,"noneFound":true,"element":null}')

Now we can respond to this particular error by opening Notepad and trying to input again.

Tips and tricks to handle exceptions

It is recommended to follow these practices when handling exceptions:

  1. Try to predict possible behaviour of a program and process each scenario separately if it makes sense.

  2. When using the 'Error' port, it is useful to store the error message in a variable and print it to a log file. A friendly message can be prepared for a business-user.

  3. Sometimes it makes sense to put a part of a workflow to a separate subprogram with no exception handling inside the subprogram and use the 'Error' port of the subprogram to handle exceptions.

  4. A good practice in some cases is catching an exception and returning to the initial state of the workflow. For example, if a workflow involves data migration from a set of documents to a certain software, then a good solution to an exception occurring during an interaction is to close (or kill the process) the program and return it to its initial state.

  5. In some cases a developer can generate an exception on its own. The 'Execute JS code' activity can be used for that. To throw a custom exception there should be written the following: throw new Error("Here goes the text to be shown when this exception occurs")