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.
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.
Suppose we want to input some text to the edit field of the notepad application in Windows.
Open the notepad using any of the available methods, for example, by pressing 'win + r' - typing 'notepad' - pressing 'enter'.
Return to 'DevTool' and add the 'Input from keyboard' function of the 'Interface elements' group to the workspace.
In the parameters section of the added block, press 'Change the element' button.
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.
Press 'Ok' in the 'Element Picker' and return to the development environment.
Enter any text to the 'Value' field, for example,
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 blocks following the green port they wouldn't be performed.
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.
Add the 'Assign value to variable' block and connect it with the 'Error' port of the 'Input from keyboard' function.
Make sure that the parameters of the added block appeared on the right panel.
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).
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 type
An error occurred while working with the notepad.
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.
It is recommended to follow these practices when handling exceptions:
Try to predict possible behavior of a program and process each scenario separately if it makes sense.
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.
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.
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.
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")