Exceptions handling

Principles of exceptions handling

Introduction

While creating algorithms, a developer should consider various scenarios and be able to handle them. If it's possible to predict the program behavior, then this is desirable to handle it directly. 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 didn't appear). Sometimes this can be processed separately. But if an algorithm consists of numerous consecutive clicks or types from the keyboard then this is pretty time-consuming to handle each step let alone that the algorithm may become very hard to read. In that case a common solution is to put these blocks to a subprogram and handle the possible exception on a level of subprogram.

Handling mechanism

Each block in the 'electroNeek DevTool' contains the 'Error' port. If no branch comes out of this port, then an exception occurred during the execution of this block will lead the algorithm to stop and the corresponding error message will be thrown. If the 'Error' port has a branch coming out of it then the algorithm will perform the steps on the branch and won't stop with an error message.

Example without exception handling

Suppose we want to input some text to the edit field of 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 'DevTool' and add the 'Input from keyboard' function of the 'Interface elements' group to the workspace.

  3. In the parameters section of the added block, press 'Change the 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.

Selector of the edit field of the 'Notepad'

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 blocks following the green port they wouldn't be performed.

Error message when the notepad is closed

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' block and connect it with the 'Error' port of the 'Input from keyboard' function.

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

  3. Type exception 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 block with another 'Assign value to variable' block. 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 algorithm 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.

The result of the algorithm with exception handling

Tips and tricks to handle exceptions

It is recommended to follow these practices when handling exceptions:

  1. Try to predict possible behavior 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 an algorithm 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 algorithm. For example, if an algorithm involves data migration from a set of documents to a certain software, then a good solution to an exception occuring 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' block 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")