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

13 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Andrew Xu
Andrew Xu
10 months 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
10 months 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
9 months 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
8 months 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
7 months 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 month 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
30 days 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