Power Automate Exception handling using Try Catch Block

In this Power Automate tutorial, we will see how to handle exceptions using Try Catch Block in Power Automate or Microsoft Flow. We will see Power Automate Exception handling using Try Catch.

To see how to handle errors in Power Automate, we will take an example, where we will create a Microsoft Flow, that will get the data from Excel and create items in the SharePoint list.

After running the flow, if there will be any error, in any action used in a flow, to track the error we will create a try-catch block by using the ‘Scope‘ action and functionality ‘Configure run after‘.

In this example, we are going to use Task Management as a SharePoint list, which contains three columns:

  • Title- Single line of text.
  • Assignee: Single line of text
  • Assignments: Choice
exception handling in power automate
exception handling in power automate

We will use the below Excel workbook which contains the same 3 columns as the SharePoint list.

Power Automate exception handling
Power Automate exception handling

And we are going to create the below flow where we can handle the exception when the error occurred using the try-catch block.

Power Automate exception handling using try catch
Power Automate exception handling using try-catch

Let’s see how we can create flow in Power Automate.

Power Automate Exception handling using Try Catch Block

Here we will create a flow that will handle exceptions using the Try Catch block and notify us in Outlook using Power Automate.

To create the flow in Power Automate, follow the below steps:

Step 1: Now we will create a flow that will manually trigger, with a button click in Power Automate, and it will create an item in the SharePoint list from Excel.

So, Login to Power Automate, click on the +Create icon, from the navigation menu, and then select ‘Instant cloud flow‘.

Power Automate exception handling using try catch block
Power Automate exception handling using try-catch block

Next, provide the Flow name and then select the trigger as Manually trigger a flow. Click on Create button

Power Automate error handling using try catch block
Power Automate error handling using try catch block

Now you can see Flow get created with the triggered action ‘Manually trigger flow‘.

power automate error handling
power automate error handling

Step 2: We will initialize the string variable, which will use later to store the Assignments value. So click on the +Next step. Select Initialize Variable action and then provide the below information.

  • Name: provide the variable name.
  • Type: select type as String
power automate catch error message
power automate catch error message

Step 3: Now we add the Scope action, inside that we will add all the actions that will read the data and create items in the SharePoint list.

So, click on the +Next step and select Scope action and you can see the Scope action is added to the flow.

power automate scope error handling
power automate scope error handling

Now we will rename the Scope action to Try, so, click on the More icon() and then click on Rename.

power automate try catch block
power automate try catch block

Step 4: Now we will get the data from Excel, so for this, we will add the action ‘List rows present in a table’.

See also  Create SharePoint Online List and Columns from Excel Using Power Automate [Download Complete Flow]

So click on ‘Add an action‘ inside the Scope block (Try), and then select the action ‘List rows present in a table’. Then provide the below information:

  • Location: Provide the location where the Excel file is located.
  • Document Library: Provide the library where the Excel file is located.
  • File: Select the Excel file from which you want to create the item in the SharePoint list
  • Table: Select the table from the Excel workbook.
 error handling in power automate
error handling in power automate

Step 5: To read each row in Excel, we are going to use the ‘Apply to each’ action.

So, click on the ‘+Add an action‘ and then select ‘Apply to each‘. Then provide the below information:

  • Select an output from previous steps: select ‘ value'(List rows present in a table) from the dynamic content.
exception handling using try catch in power automate
exception handling using try catch in power automate

Step 6: Next we will set the variable with the value of assignments, so this value we will use while creating items in the SharePoint list.

So, click on Add an action, inside the ‘Apply to each’ action and then select the Set variable action. then provide the below information.

  • Name: Select the name of the variable(Assignments).
  • Value: Select the Assignments(List rows present in a table) from the Dynamic content.
 catch error message in power automate
catch error message in power automate

Step 7: Now we will create an item in the SharePoint list, for this we will select the action as ‘Create item’. For this, click on ‘Add an action‘, and then select ‘Create item‘. Then provide the below information:

  • Site address: Select the site address of the List
  • List name: Select the list where you want to create an item
  • Title: Select Title(List rows present in a table) from the dynamic content
  • Assignee: Select Assignee(List rows present in a table) from the dynamic content
  • Assignments: In this click on the dropdown -> select custom value -> select Assignments(variable) from the dynamic content.
 scope error handling in power automate
scope error handling in power automate

Now our Try block is ready, to handle and get the error message, if any error occurs in the Try block, for this we need to create a catch block.

Step 8: Now we will create a Catch block to handle the error, by using the functionality ‘Configure run after’.

So click on the +Next step, then select the ‘Scope‘ action and rename it to ‘Catch‘ block.

scope exception handling in power automate
scope exception handling in power automate

Now we will use the functionality ‘ Configure run after’, to handle exceptions in the Try block.

See also  How to Create a Word Document from a SharePoint list item using Power Automate?

Configure run after in Power Automate is a functionality, which allows you to do certain tasks after the action is successful or failed. In this case, if the Try block action is failed, skipped, and timeout then only the Catch block action will run and notify which action is failed.

So click on the More icon() -> click on ‘Configure run after‘, as you can see in the below image.

scope exception handling using try catch in power automate
scope exception handling using try catch in power automate

Now you can see the below window ‘Catch’ should run after, here select ‘ has failed, is skipped, and has timed out‘. Then click on Done.

power automate failure notification
power automate failure notification


Step 9: To get the details of what happened to the action inside the Try block, for this we will use the ‘Filter array’ action.

So, click on the Add an action -> select Filter array action. Then provide the below information:

  • From: In this provide the below expression:
result('Try')
  • To add the condition, click on ‘Edit in advance mode‘ and then provide the below expression:
@or(equals(item()?['Status'], 'Failed'), or(equals(item()?['Status'], 'Skipped'), equals(item()?['Status'], 'TimedOut')))
power automate scope error message
power automate scope error message

Step 10: Now we will create HTML table to get the error message, for the action that is failed or skipped or timed out, then the status of the action and error message if the action failed.

So here click on Add an action, -> select Create HTML Table -> then provide the below information:

  • From: select the Body(Filter array) from the dynamic content
  • Header: In this, provide the below information:
Action@item()?[‘name’]
Status@item()?[‘Status’]
ErrorMessage@item()?[‘error’]?[‘message’]
Headers of the Html table
handle exception in Microsoft Flow
handle exception in Microsoft Flow

Step 11: Now we will format the HTML table, so for this click on Add an action -> select compose action -> rename it to Format Html table.

Then provide the below information:

  • Inputs: provide the below expression to apply CSS to the HTML table.
replace(body('Create_HTML_table'), '<table>', '<table border="3">')
handle exception using try catch block in Microsoft Flow
handle exception using try catch block in Microsoft Flow

Step 12: Now we will send an email of error details to the flow owner, so, for this, click on Add an action -> select Send an email(V2). Then provide the below information:

  • To: Provide the email of the receiver, who will receive the error details.
  • Subject: Provide the subject of the email
  • Body: In the body we will add the Html table, as well we will provide the URL of the flow, so the user can come back to the flow directly from email, to check the error. So click on this’ </>’ to enable the HTML table, and provide the below information: replace’expression’ with the below expression.
<p>@{outputs('Format_Html_table')}<br>
<br>
</p>
<a href='expression'>Check Flow Run </a>

//expression =concat('https://make.powerautomate.com/environments/Default-f0e4273f-10c1-4551-a494-977ebeb5b5fa/flows/764a1599-3405-4323-a6e8-c3df7ec44858?backUrl=%2Fenvironments%2FDefault-f0e4273f-10c1-4551-a494-977ebeb5b5fa%2Fflows%2F764a1599-3405-4323-a6e8-c3df7ec44858%2Fdetails', workflow()?['tags']['environmentName'], '/flows/', workflow()?['name'], '/runs/', workflow()?['run']['name'])
catch exception message in power automate
catch exception message in power automate

Now our catch block is ready, so it’s time to test the flow, whether it is working perfectly or not.

See also  How to Update Items in a SharePoint List in Power Automate Desktop?

Step 13: Now our flow is ready, we will run the flow and will check the flow in two scenarios:

  • When flow runs successfully
  • When flow get failed

When flow runs successfully

Here to run the flow and click on the Test icon -> Manually -> click on Save and test button -> In the Run Flow window, click on the Run flow button -> click on the Done button.

You can see your flow run successfully, and it doesn’t come to the catch block, because there is no error.

How to handle exception in Power Automate
How to handle exception in Power Automate

As our flow ran successfully, we can see in SharePoint list items get created.

How to handle exception using try catch in Power automate
How to handle exception using try catch in Power automate

When flow get failed

Here we will change the Assignments variable type string to integer, as the value is of string type, so it will throw an error.

How to do exceptional handling in Power Automate
How to do exceptional handling in Power Automate

Here to run the flow and click on the Test icon -> Manually -> click on Save and test button-> in the Run Flow window, click on the Run flow button -> click on the Done button.

Now you can see Try block failed due to an error, and the catch block is run successfully.

How to handle exception in Microsoft Flow
How to handle exception in Microsoft Flow

Now you can check the outlook, you will get an email having the error details.

How to handle exception using try catch in Microsoft Flow
How to handle exception using try catch in Microsoft Flow

Conclusion

In this Power Automate tutorial, we saw exception handling in Power Automate using the try-catch block.

Also, we saw how we can create a try-catch block in Power Automate, that will notify when an error occurred in Power Automate Flow.

You may also like the following tutorials:

>