Exception handling in power automate using Try Catch Finally Scope control

Discover exception handling in power automate using Try Catch Finally Scope – in 3 steps instantly

No comments

 349 total views,  3 views today

In this exception handling in power automate article, we will learn how to handle exceptions in power automate using Try Catch finally Scope. Like any other traditional programming language, Power Automate does have an exception handling mechanism.

We know what it means by exception handling – basically, in the exception or error handling mechanism we have three blocks Try, Catch, and Finally.

In the Try block, we keep the code that might through the error, the Catch block will catch those errors, and in the Finally block, we take the action for those errors, this is how the exception handling mechanism works in all traditional programming languages.

Syntax: Exception handling in Programming language


Try

{

Keep the code here.....

}

Catch

{

Catch the exception from the above-executed codes...

}

Finally

{

Do something for the exception or error

}

Exception handling in power automate using try catch finally

The way we saw the exception handing in traditional programming languages, in Power Automate also we can handle the exception when an action failed to run or throws an exception using the “Scope” action. Here we will learn how to handle the exception or error using the Power Automate “Scope” action like the Try, Catch and Finally block.

Let us get into that. We are going to add the scope action in one of our existing Power Automate, here Power Automate is not important, it could be any Power Automates, let’s not focus on the Power Automate, only focus on the scope actions.

Exception handling in Power Automate using the scope action
Exception handling in Power Automate using the scope action

Let’s expand and explain each action one by one.

Step 1: Exception handling in Power Automate using the scope action – create a boolean variable

Add an action “Initialize variable”.

Create a Boolean type variable and assign the “false” as a default value.

Exception handling in Power Automate using the scope action - create a Boolean variable
Exception handling in Power Automate using the scope action – create a Boolean variable

Step 2: Exception handling in Power Automate using the scope action – add 3 scopes actions in sequences

Add 3 scope controls in a sequence one by one.

For our better understanding rename the scopes as Try, Catch, and Finally something like that.

Configuration of the Catch Scope control (Exception handling in Power Automate)

Now, go to the second scope control i.e. meant for the catch block.

Click on three dots.

Exception handling in Power Automate using the scope action - Configure Run after settings
Exception handling in Power Automate using the scope action – Configure Run after settings

Click on the “Configure run after” settings.

Configure Run After settings, scope try failed timed out in Power Automate
Configure Run After settings, scope try failed timed out in Power Automate

In this “Configure run after” configuration – basically, it says when this scope Catch should run. It will run if the Try Scope has the following output and if we configure these checkboxes in the Catch scope:

  • is successful
  • has failed
  • is skipped
  • has timed out

Note:

  • Generally, we configure “has failed” and “has timed out”.
  • If your flow doesn’t have errors at this moment, for the debugging purpose, you can select “is successful” as well, so that you can test that your Catch scope is executing, later on, you can uncheck the “is successful

Then, click on the “Done” button.

Configure Run After settings, scope try failed timed out in Power Automate - Catch Scope
Catch Scope configuration in Power Automate

Inside the “Catch Scope” action, add a new action “Set variable” and select your boolean variable created above “isErrorOccurred” and set the value to “true” – this means an error has occurred inside the “Try Scope” block. That’s it from the Catch Scope configuration.

By now, the configuration of the Catch block is done, now let’s go to the Try Scope configuration.

Configuration of the Try Scope action (Exception handling in Power Automate)

Just click on the first scope action, i.e. meant for the Try Scope action.

Exception handling in Power Automate using the scope action - Try Scope Configuration
Exception handling in Power Automate using the scope action – Try Scope Configuration

Add the actions for which you want to handle the exceptions.

Notes:

  • We can add a collection of actions (more than one action).
  • If you are adding the scope action at the time of creating your flow, you can directly add it inside the Try scope action and configure it or if you are handling the exception for your already developed flow, then you just can drag and drop those actions into the Try Scope section. In this demo, we have just used the existing actions by the drag and drop.
  • Dependent action cannot be kept inside the same Try Scope, for example, let us say you have two actions 1)Update Item and 2)Create Item. If the “Create Item” is dependent on the “Update Item” output, then we cannot keep the “Update Item” action and “Create Item” action inside the same Try scope – it will not allow adding (will throw an exception while adding).

Configuration of the Finally Scope action (Exception handling in Power Automate)

Inside the Finally Scope action, add a “Condition” control and the below condition:

  • if “{x}isErrorOccurred” is equal to true
Configuration of Finally Scope action in Power Automate
Configuration of Finally Scope action in Power Automate

If the condition returns true, then the error has occurred in the Try Scope block, if returns false then there was no error found in the Try Scope block, which means the flow has run successfully.

While failing the flow or error occurred, we can notify to flow owner using any of the below actions:

  • Send me an email notification or
  • Send me a mobile notification

In this demo, we have used the “Send me an email notification” action, configurations are as below:

Configuration of Finally Scope action in Power Automate - send me an email notification
Configuration of Finally Scope action in Power Automate – send me an email notification

By now, we have successfully implemented the Try, Catch, and Finally Scope action in this demo, and now we can go ahead to test it.

In this flow where we have implemented the exception handling, there are no errors, so, we cannot test the error condition, to test the sequences, we need to enable the “is successful” in the Catch Scope action – configure run after.

Configure Run After settings, scope is successful in Power Automate - Catch Scope
Configure Run After settings scope “is successful” in Power Automate – Catch Scope

Just check the “is successful” checkbox and click on the “Done” button. In this configuration, we are saying execute the catch scope block when the Try Scope block: is successful, has failed, has timed out.

Since our flow (Try Block actions) is not failing or timed out, but successfully running, the Catch Scope block will also be running. But in real-time, we don’t check the “is successful” setting.

Now, let’s run it using the test mode from the flow designer’s top right-most corner.

Test Flow using Power Automate Designer
Test Flow using Power Automate Designer

We could see flow ran successfully.

Test Flow using Power Automate Designer - Flow ran successfully
Test Flow using Power Automate Designer – Flow ran successfully

Let’s expand the “Scope – Finally” block.

Exception handling in Power Automate using the scope action - Demo
Exception handling in Power Automate using the scope action – Demo

We could see the condition expression result is “true” for the “isErrorOccured” variable because we have checked the checkbox for “is successful” also in the “Catch Scope”, which means “isErrorOccured” will be set to “true” even though the “Try Scope” runs successfully.

How to handle multiple Try Catch Finally Scopes in Power Automate?

Let’s say due to the business logic you need to have multiple scopes control in your flow for a different type of logic, then how to handle it?

For example, first, if you have a Get Items action in your flow, which is reading an item from the SharePoint list, then you have an update item action in your flow – the business logic is if your Get Items operation is successful, then only update item should be executed.

For this scenario, add three scopes (Try, Catch, and Finally) controls for the “Get Items” action, at the Finally Scope of the “Get Items” action add a condition control to check for the error, if an error is found, at the yes part of the condition add a notification action to notify the flow owner, and if the error is not found, at the “No” part of the condition control handle the update operation – in the “Update an Item” action implement try catch finally block using the as usual scope control (exactly the above demo).

Summary: Exception handling in power automate (Try Catch Finally in Power Automate)

Thus, in this article, we have learned how to handle the exception in Power Automate using the scope control exactly like the Try, Catch and Finally block.

See Also: Power Automate Tutorial

You may also like the below Power Automate Tutorials:

 

About Post Author

Do you have a better solution or question on this topic? Please leave a comment