A common programming practice is to implement error handling to ensure the robustness of the application and in the Server-Side development using C#, we used to use Try Catch Finally blocks to apply the error handling mechanism. In this article, we will see how to do something similar in Power Automate
In case of any error that happens within power automate, the flow errors out without any native way to intimate the user of a specific issue that happened inside the flow. The out of the box error intimation happens when multiple such failures happen, and it may not be a robust way to deal with errors in a highly available application.
As a workaround, we will use the Scope action to define a scope each for Try, Catch, and Finally where the logics will reside and define their execution conditions so that they get called when they must.
Let’s create a Power Automate that is triggered manually for testing the implementation. As the first step, we will define a variable varIsError that will be used to store the error flag if an error has occurred and initialize it with a default value of false.
Followed by that we will add 3 scope actions each both Try, Catch, and Finally
Within the try scope, we will have the major flow logics that need to be checked for any possible errors. For time being we have added an HTTP Request action, WSand, in the URI, we have given a list name that does not exist so that it will error for sure to see how the control will flow.
Followed by this, let’s add the Catch Scope so that whenever the Try Scope errors out, the Catch Scope will be called. Inside the catch, we will set the varIsError variable to true so that we will evaluate this variable in the finally scope to take the needed action.
But we must ensure that the catch scope is called only on the failure of try scope. To implement this conditional call, we will click on the 3 dots -> Configure run after which will open the below window where we will check the has failed check box so that Catch Scope gets called only when Try Scope is failed.
As the last action, we will add another Scope that will contain the actions that need to be executed in the Finally block. Here we will check the varIsError variable to see if any error has occurred in the previous Try scope and based on that we will branch the control flow. In case the variable value is true, it means the error has happened and the value was set inside the catch scope and hence it will send a mail stating the occurrence of the error to the stakeholder.
In case the variable contains the false value, it will send out a mail stating the success of the flow and share the count of items retrieved by the Get Items action using the below expression in the mail body:
So the overall flow would look like this:
Test the flow
Now let’s test the flow by manually triggering it which will call the HTTP action in the Try Scope which will error out as we have given a nonexisting list name in the GET URI.
When this error happened, the Catch scope was executed and the error flag was set to true which is then checked in Finally block, and an error intimation mail is sent to the user.
Now that we have checked the error flow, let’s update the HTTP Action in the Try scope with a valid list name to test the positive control flow.
On retriggering the flow, we can see that the Try Scope was completed successfully due to which the Catch scope was not called and the Finally Scope is called where we have retrieved the item count and sent the success intimation to the user.
Thus, we saw how we can use the Scope action within Power Automate to simulate the Try Catch Finally block in .NET Framework to implement a more robust exception handling within the flows.