When developing an algorithm, the developer often has to consider the program's behavior in different scenarios and think through the steps for handling possible errors in the bot's operation. For example, if the bot is supposed to work with files on a computer, a situation can arise when the necessary file or folder is missing (has been moved or deleted). This scenario (depending on the business process, of course) can usually be foreseen and handled quite easily, for example, by adding an activity to check the presence of a file before the activity that works directly with that file.
However, there may be exceptional situations that cannot always be foreseen. For example, when working with the interface of the application or browser, it may happen that some element (a button or a picture) will not load in time or will not appear at all. And when the algorithm reaches the step of working with this activity, an exception will occur, because the bot will not be able to find the element in the given time. There are several ways to handle such exceptions, we will describe them in more detail below.
Exceptions handling mechanism
Each activity block has an "Error" port, or "red port" as it is called. This is the port to which you can bind a set of activities to be performed if an error occurs during the main specified activity step.
Let's take a simple example of error handling in the execution of an algorithm.
Suppose that our bot has to read text from some file. To do this, we use the Read text file activity, but deliberately move the file itself before launching the bot, so that the bot won't be able to find it. For some time the bot will try to find the required file, but will eventually stop and an error message will appear in the console:
Now let's look at the exception handling process itself. Let's design the algorithm so that the bot displays the cause of the error in the notification window for the user.
- Specify a path to file in the Read text file activity. The path should look like this:
- Move this file to some folder so that the path to the file changes, but we won't change the "Path" parameter in the activity parameters.
- Add the Assign value to variable activity via the red port. You can set any name for the variable, for example "exception". In the "Variable value" parameter, pick the "Save the previous step result" option.
- The next acivity in the workflow will be the User notification activity. Pick the "Save the previous step result" option in the "Description message" parameter. You can specify any value as a "Button name" parameter.
Run the algorithm. The bot will try to find the file, but when it fails, there will appear a message showing that there is no such file in the given path.
In reality, such a scenario can very easily be foreseen and handled (by checking the presence of the file manually). In this example, we want to demonstrate an approach that could be used in such a scenario if the manual check cannot be performed before launching the bot, so you can see on a simple example the principle of exception handling.
Useful tips for exceptions handling
The following tips and tricks are recommended when processing exceptions:
- Try to anticipate possible bot behavior and, where it makes sense, handle each scenario explicitly.
- When using the "Error" port, it is often useful to save an error message and display it in the log or in a message to the user. For the business user, you can provide a clearer error message.
- In some situations, it makes sense to allocate the part of the algorithm into a subprogram, within which not to handle exceptions. It is better to use the "Error" port from the subprogram block to handle exceptions.
A good practice in some cases is to catch an exception and return the algorithm to its starting point for another pass. For example, if the algorithm involves transferring data from a set of documents into a program, a good solution when an exception occurs at any iteration is to close the program (or force termination) and return to the initial state of the algorithm to re-process that document or move on to the next.
- If your algorithm interacts with the interface of a website or some application, it would be useful to add the Take a screenshot activity via the red port. Thus, when an exception occurs, you can not only see the error message in the console, but also a screenshot of the moment the error occurred, which will allow to understand the causes and handle the exception in more detail.