Do you want to learn about error handling in Power Automate flow? In this flow tutorial, I will explain exception handling in Power Automate cloud flow. I will show you how to use try catch finally in Power Automate. Finally, I will show you how to do error handling in apply to each in Power Automate.
Sometimes, the flow has failed because of an error. To resolve it quickly, we will notify the user that the error occurred so that it will be easy for the user to configure and fix it. For that, we are going to use a try-catch block by using the ‘Scope‘ action and functionality ‘Configure run after‘.
- Try – In the try section, we will add a flow action to execute.
- Catch – In the catch section, we will add a flow action that fails, to handle the error.
- Finally – It executes an action.
Scenario:
Here, we will cover two examples to handle exceptions; in the first example, The flow triggers manually and requests the user to enter the numerator and denominator value, and in the try section, it performs the function. If any exception occurs in the try section, the catch section executes. Finally, it notifies the user.
In the second example, The flow triggers manually, and then we initialize a variable IsError; by default, set the value to false. In the try section, use the Get all lists and libraries action and provide the SharePoint site Address; it retrieves the information from the selected site. If any exception occurs in the try section, it jumps to the catch section and sets the variable value as true. Finally, using the Terminate action it displays the status as Failed or Succeeded
Follow the below examples to handle exceptions using the Try Catch block and notify users in the Power Automate flow.
Power Automate Error Handling try-catch
Let us see how to add a handle and recover from unexpected errors in a flow using the try catch finally in Power Automate.
Follow the below steps to create a flow to handle exceptions in Power Automate:
Example -1:
Step-1:
Login to Power Automate, click on the select + create option, choose the instant cloud flow, enter the flow name, and click on the create option.
Step-2:
Expand the trigger action and pass the two number input values for the Numerator and Denominator as below:
Step-3:
Select +new step, add a scope action, and rename it as Scope – try action. Then, add a compose action inside the scope and pass the below expression as an input value, which performs the division based on the two input values.
div(triggerBody()['number'], triggerBody()['number_1'])
Step-4:
Add a new step, select scope action, rename it as a Scope-catch action, and send an email (V2) action to notify the user that the flow has failed.
- To – Dynamically select the User’s email
- Subject – Provide the subject of the email
- Body – Enter the body of the email
Now we will use the functionality ‘Configure run after’, to handle exceptions that occurred in the Try block.
- Configure run after in Power Automate is a feature that allows us to do certain actions after an action has been completed or failed.
- If the Try block action fails, is skipped, or times out in this situation, just the Catch block action will run and notify.
- To enable it, select the ellipsis -> click on ‘Configure run after‘, as you can see in the below image.
Here, choose the ‘has failed’ option and click on Done.
Step-5:
Add a +new step. Select a Delay action from action triggers and pass the count as 10 and the unit as second.
Similarly, configure the configure run after functionality to handle exceptions for the catch block in the delay action as below, And click done.
If the catch block is successful, it will not come to the delay action. The delay action is performed only when the catch block has failed, is skipped, and has timed out.
Step-6:
Add a new step, send an email(V2) action, and pass the values for To, subject, and Body to notify the user that the flow has succeeded.
Our complete flow looks like below:
Test Case-1:
Save and Run the flow manually, Enter the numerator and denominator value, and choose the Run flow option.
Once the flow runs successfully as below:
The user will receive an email in Outlook like the below:
Test Case-2:
The flow triggers manually. Enter the numerator and denominator value, and choose the Run flow option.
Once the flow runs successfully, like below:
The user will receive an email in Outlook like below:
This is how to add a handle and recover from unexpected errors in the Power Automate flow.
Example -2:
In this example, we will add a Try Catch Finally block to handle exceptions inside the flow.
Follow the below steps to achieve this:
Step-1:
In the Power Automate home page, click on the select + create option, choose the instant cloud flow, enter the flow name, and click on the create option.
Step-2:
Add a new step, select the initialized variable action from action triggers, enter the variable name, and choose the variable type as boolean. By default, set the value as False.
Step-3:
Add a new step and select the scope action, rename it as a Scope- Try. Then, add a Get all list and libraries action inside the scope. Provide the SharePoint site address to retrieve information from the selected site.
Step-4:
Add+ new step and choose the scope action from action triggers and rename it as a Scope- catch. Then, add a set variable action inside the scope catch action.
From the dropdown, select the initialized variable (i.e., Step 2) and set the value as true under expression.
Now we have to use the ‘Configure run after’ functionality.
- If the Try block action fails, we will handle the exceptions in the catch block
- To enable it select the ellipsis -> click on ‘Configure run after‘, as you can see in the below image.
Here, the catch action performs; only the scope try has failed and has timed out. So click on the Done option.
Step-5:
Click on the new step and a scope action from action triggers and rename it as a Scope- Finally. Click on the add an action, and select condition action from action triggers IsError is equal to True.
If yes, select the Terminate action from action triggers and set the status Failed. If No, select the terminate action from action triggers and set the status as Succeeded.
Now, we have to use the ‘Configure run after’ functionality.
- Based on the outcome of the Try block action and catch block, Configure run after for that select the ellipsis -> and select all the options, and click on configure run after like below:
The Scope Finally action is performed when the scope catch action is successful, has failed, is skipped, and has timed out. Select all the options and click Done as shown below:
Our completed flow looks like the following:
Step-6:
Save and test the flow by selecting the Test -> Run flow option.
Test case -1:
Here, I have selected the valid SharePoint site address from the dropdown, so it retrieves all the lists and library information for the selected site in the try block without any exceptions.
Our flow ran successfully and returned the terminate status as succeeded.
Test case -2:
Here I have passed the Invalid SharePoint site address, so when the flow runs, it handles the exception in the catch section.
Our flow ran successfully and returned the terminate status as Failed.
This is how to add a Try Catch Finally block to handle exceptions using the flow.
Power Automate Error Handling in apply to each
Let us see how to do error handling in apply to each in Power Automate.
Yes, it is possible to handle errors in apply to each control using a flow. So in this example, we will use the below Excel workbook to loop through the rows presented in the Excel table.
We will check the condition of the DOB is equal to today’s date, and then it will send an email to the particular user.
Follow the below steps to handle errors in apply to each control using flow:
Step-1:
In the Power Automate home page, create an instant cloud flow, enter the flow name, and click on the create option.
Step-2:
Add a new step and select the initialized variable action from action triggers, enter the variable name, and choose the variable type as boolean. By default, set the value as False.
Similarly, Select a new step and add two more initialize variable actions; for the DOB and Email ID, select the variable type as String, and leave the value section blank.
Step-3:
Add a new step and select the scope action rename it as a Scope- Try. Then, add select List rows present in a table action from action triggers; provide the below-required parameters.
- Location – Select the Location where the Excel file exists
- Document Library – Choose the document library from the dropdown
- File – Select the Excel file ( I have selected my Employees Birthday Table)
- Table – From the dropdown, choose the table name
Add an “apply to each” control loop and pass the input as dynamic content values.
Inside the loop, add a set variable action and in the name section from the dropdown, select the initialized variable (DOB). In the value section, pass the dynamic content of the DOB value.
Add a compose data operation and pass the below expression, Just because from the Excel workbook date column returns some random integer value. To display that in the date time format use the below formula.
addDays('1899-12-30',int(variables('DOB')),'yyyy-MM-dd')
Inside the loop, add a condition control to check if the DOB column is equal to today, If Yes, set the ISError variable as false by using the set variable action.
formatDateTime(utcNow(), 'yyyy-MM-dd')
Similarly, add a set variable action, and in the name section from the dropdown, select the initialized variable (Email). In the value section, pass the dynamic content of the Email value.
If No, leave it blank.
Step-4:
Add+ new step and choose the scope action from action triggers and rename it as a Scope- catch. Then add a set variable action inside the scope catch action.
From the dropdown, select the initialized variable (i.e., Step 2) and set the value as true under expression.
Now, we have to use the ‘Configure run after’ functionality.
- If the Try block action fails, we will handle the exceptions in the catch block
- To enable it, select the ellipsis -> click on ‘Configure run after‘. Here the catch action performs; only the scope try has failed, is skipped, and has timed out. So click on the Done option.
Step-5:
Click on the new step and a scope action from action triggers and rename it a Scope- Finally. Click on the add an action, and select condition action from action triggers IsError is equal to True.
If yes, select the Terminate action from action triggers, and set the status Failed. If No, select the send an email(V2) action from action triggers and pass the required fields.
- To – select the Email Variable from the dynamic content
- Subject – Provide the Subject of the mail
- Body – Enter the body of the mail
Now, we have to use the ‘Configure run after’ functionality.
- Based on the outcome of the Try block action and catch block, Configure run after for that, select the ellipsis -> and click on configure run after.
- The Scope Finally action is performed when the scope catch action is successful, has failed, is skipped, and has timed out. Select all the options and click Done as shown below:
Our completed flow looks like the following:
Step-6:
Save and test the flow by selecting the Test -> Run flow option.
Test case -1:
Here, I have passed the correct formula in the compose data operation to retrieve the date column value from the Excel table in the try block without any exceptions. Once the flow ran successfully, like below:
The employees will receive birthday emails as per the birth date presented in the Excel table workbook in Outlook like below:
Test case -2:
In the Scope try block, in the compose data operation, add the below formula as an input, so that the try action fails and jumps to the catch block. Just because by default from the Excel table date column throws a random integer value.
formatDateTime(variables('DOB'),'yyyy-MM-dd')
Save and run the flow. Our flow runs and returns the terminate status as Failed.
This is how to handle errors in apply to each control using Power Automate flow.
Conclusion
I hope now you have learned how to implement exception handling in Power Automate. I have explained, with examples, how to use try catch finally in Power Automate and how to do error handling in apply to each in Power Automate. Whenever you are creating a flow using Power Automate, you should know how to handle errors in Power Automate and how to use try catch finally in power automate.
You may also like:
- Send an Email When an Item is Deleted in SharePoint via Power Automate
- Update SharePoint list item only one field using Power Automate
- How to Run a Query against a Dataset Using Power Automate?
- Power Automate Condition Based on Form Response Example
- Populate a Word Template using Power Automate
- Power Automate: The credentials for this connection do not have access to. Please try using a different location or account
After working for more than 15 years in Microsoft technologies like SharePoint, Office 365, and Power Platform (Power Apps, Power Automate, and Power BI), I thought will share my SharePoint expertise knowledge with the world. Our audiences are from the United States, Canada, the United Kingdom, Australia, New Zealand, etc. For my expertise knowledge and SharePoint tutorials, Microsoft has been awarded a Microsoft SharePoint MVP (9 times). I have also worked in companies like HP, TCS, KPIT, etc.