3 Power Automate Error-Handling Patterns You Must Know

3 Power Automate Error-Handling Patterns You Must Know

It is important to include error-handling in every Power Automate flow. When a flow fails, the owner should be notified that the flow failed. The message should say why the flow failed and identify the flow run. In this article I’ll share 3 Power Automate error-handling patterns you can re-use in any flow you build.



Table Of Contents:

Error-Handling Pattern #1: Try, Catch Finally Pattern
β€’ Try Action
β€’ Catch Action
β€’ Finally Action
β€’ Test The Try, Catch, Finally Pattern

Error-Handling Pattern #2:  Terminate Flow Success or Failure Pattern
β€’ Test The Terminate Flow Pattern

Error-Handling Pattern #3: Get Flow Run Details URL Pattern
β€’ Build The Flow Run Details URL
β€’ Send Failure Notification Email With Flow Run Link




Error-Handling Pattern #1: Try, Catch Finally Pattern

We will build a basic flow to learn the first Power Automate error-handling pattern. This pattern, called try, catch, finally, is found in many other programming languages.

  • Try – attempt to execute a flow action
  • Catch – if the flow action fails, do this action to handle the error
  • Finally – run this action regardless of the result




Try Action

Create a new flow with an instant trigger and add a Compose action.



Then write this expression inside the Compose action to cause an error. Since the number 1 cannot be divided by 0 the flow action will fail.

div(1,0)




Catch Action

Next, initialize a variable called varFlowRunFailed to track if the flow failed.



We only want to run the initialize variable action to if the previous action failed. Click the three dots on the initialize variable action and select Configure run after.


Setup the configure run after options as shown below. If the previous flow action fails, the Initialize variable action will run. Or if the previous flow action succeeds, this action will be skipped.




Finally Action

Insert an Office 365 Outlook: Send an email (V2) action to send a notification that the flow completed.



The last step on our Try, Catch, Finally pattern will always run regardless of whether the Compose (try) action succeeded or failed. Go to the configure run after menu and check all of the checkboxes.




Test The Try, Catch, Finally Pattern

To see how the try, catch, finally pattern works, give the flow it a test run. The Compose (try) action will fail, the Initialize variable (catch) action handles the error and the Send an email (finally) action runs afterwards.




Error-Handling Pattern #2: Terminate Flow Success or Failure Pattern

When the test flow run is completed we see the notification “your flow run successfully” appear. This is not true. Our flow’s Compose action failed so the notification should say “flow run failed” instead. Next we’ll improve our Power Automate error handling to show the proper result.

Remove the Send an email (V2) action. Then replace it with the Condition action where the variable varFlowRunFailed is equal to true.



Check all of the checkboxes in the configure the Condition action’s run after settings.



If varFlowRunFailed equals true, send an email notification that the flow failed. Then insert a Terminate action afterwards. Set the Terminate status to Failed.



Otherwise, when varFlowRunFailed is not equal to true send an email and terminate the flow successfully.




Test The Terminate Flow Pattern

Run a test of the updated flow. Now the flow properly shows the message “flow run failed.”




Error-Handling Pattern #3: Get Flow Run Details URL Pattern

When a flow fails we want to inspect the flow run details to see why it failed and fix the problem. Finding the specific flow run that failed can be difficult in Power Automate. To make it easier we can send a link to the flow run details in the failure notification email.



When the email recipient opens the link they are navigated to the flow run details where they can quickly identify what went wrong




Build The Flow Run Details URL

Go to the flow’s If yes condition an add a Compose action.



Use the workflow function to get details about the workflow itself.

workflow()



Then insert another Compose action to build the flow run URL.



Flow expression #1 gets the environment’s unique identifier.

outputs('Compose:_Get_Workflow_Details')?['tags']?['environmentName']



Flow expression #2 extracts the flow’s unique identifer.

outputs('Compose:_Get_Workflow_Details')?['name']



Flow expression #3 finds the flow run’s unique identifer.

outputs('Compose:_Get_Workflow_Details')?['run']?['name']

The final step is to include the flow run url as a clickable link in the failure notification email.



Write this HTML code in the body the email and use the Compose action inside the a tag.

<a href="outputs('Compose:_Get_Workflow_URL')"&>Click here</a>to check the flow run details



Now the failure notification email contains a link to the flow run.





Questions?

If you have any questions about 3 Power Automate Error-Handling Patterns You Must Know please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest

24 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Andrew Xu
Andrew Xu
2 years ago

Another great article, thanks Matthew. I think “Or if the previous flow action fails, this action will be skipped.” should be “if the previous flow action succeeds, ……”

Asha
Asha
2 years ago

It’s great explanation really helps me a lot. As I’m new to.power platform I’m able to understand.. BTW I would like to ask can we use multiple try catch final in a single flow

For eg I have 3 conditions in my flow
1st condition I can use try catch final
2nd condition will it be possible to apply or the flow will be terminated from the 1st condition itself

Rebecca
Rebecca
2 years ago

Thanks so much. This works great for me until the final step. I struggle with powerbi and I don’t understand how you are configuring the compose actions in the Send Failure Notification Email With Flow Run Link. Are you able to put a video or exact instructions on how to enter this please? Thx so much

Andrew Butenko
1 year ago

Thanks, Matthew! This post saved me a lot of time and nerves. I owe you a beer the next time we meet. Hopefully during the MVP Summit or earlier if you’re around.

Harry D
Harry D
1 year ago

Very handy explanation!
It took me a while to figure out that the Catch step still runs because it is also set to run if the previous step is skipped.
You can extend the email message by including the error details. Use actions(‘Compose:_Test_Value’).error.message

Andrea
Andrea
1 year ago

Thanks for this, Matthew. You’ve helped me solve my immediate problem – I ended up creating a very inelegant solution with 3 conditionals to finish my flow, which is good enough for now. I will try to improve it by using Scope as you suggested to another commenter. Thanks again!

Jimmy John
Jimmy John
1 year ago

Hi Matthew, I think I am missing something in the instructions. For Initialize Variable and for the Condition both have all boxes checked for Configure After Run. Therefore the condition is always true (send the failed message). How does the variable get set to false when the flow succeeds? Thanks’ for all the great content

Joel
Joel
9 months ago

I think Jimmy John still has a point. The variable is being manually set each time to true. This flow would never work in a successful state. I recreated the flow as described and even when I set all steps to be successful it always sends the fail message. To solve this problem I had to initialize the variable at the top of the flow and then IF one of the steps failed, use “Set Variable” to change the boolean to true.

Ganesh
Ganesh
8 months ago

Hi Mathew,

I was just trying the same what you have explained. But I see that the initialize variable is setting to true always even when the Try action succeeds. And this makes the condition always to true by sending only the failure notification. Am I missing anything here?

Regards,
Ganesh a

Ganesh
Ganesh
7 months ago
Reply to  Ganesh

I resolved this issue. Thank you.

Rechard
Rechard
1 year ago

Hi I currently use item()?[‘error’][‘message’] to return the error details for the the scope results but it doesn’t always give a clear error of what failed and why is there a better way to pull the error details?

Denys
Denys
11 months ago

Thanks for detailed guideline! What if the failure appears due to connection failure? e.g. – password has been changed.
How to capture this exception?

Karen
11 months ago

If it is the connector that causes the error, ie serviceaccount needs to re-login etc. What is your recommendations of error-handling then? obviously not e-mailing, Teams-post or add row to list bc they’ll need the connection… πŸ™‚

Msb
Msb
9 months ago

If I have 10 cloud flows. Can we write a new flow which will fire on any flow fails from 10. If we have this trigger then we need not to write try catch finally in 10 flows. Matthew can help on this

Bill Dyette
Bill Dyette
3 months ago

Excellent article. My question is how can you send the error email to the person who initated the flow manually? My flow runs on a SharePoint document library and I have a few people who will run the approval flow and if they have the document open, the flow will fail. I want to notify them of the error as soon as it happens.