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.
Each activity in the 'ElectroNeek Studio' contains the 'Error' port. If no branch comes out of this port, then an exception occurred during the execution of this activity will lead the workflow to stop and the corresponding error message will be thrown. 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.
Suppose we want to input some text to 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 'Studio' and add the 'Input from keyboard' activity of the 'Interface elements' group to the canvas.
In the parameters section of the added activity, expand the Interface element dropdown menu. Then press 'Pick New 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 activities following, 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' activity and connect it with the 'Error' output of the 'Input from keyboard' activity.
Make sure that the parameters of the added activity 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 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 type
An error occurred while working with the notepad.
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.
It is recommended to follow these practices when handling exceptions:
Try to predict possible behaviour 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 a workflow 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 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.
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")