What is Error Handling?
Error handling is a way to manage errors or exceptions that happen while a program runs. The goal is to make sure the program can handle unexpected issues without crashing or giving wrong results.
In programming, we use try, catch, and finally, blocks to manage errors.
- Try Block: This contains the code that might cause an error.
- Catch Block: This handles the error if one occurs, letting you decide how to respond.
- Finally Block: This always runs, whether there was an error or not, and is useful for cleaning up resources like closing files.
Power Automate is a great tool for automating workflows, but it can also run into errors. In this blog, we'll look at how to handle these errors smoothly, record error details by adding a row to a SharePoint Excel table for tracking and send notifications to users for flow failures.
Understanding the 'Division by Zero' Error
A 'Division by Zero' error occurs when a program attempts to divide a number by zero, which is mathematically undefined. In Power Automate, this can cause your flow to fail if not handled properly.
Steps to Implement Error Handling
Create a New Flow
- Go to Power Automate.
- Click on Create and select Instant Cloud Flow.
- Name your flow and choose the Manually trigger a flow trigger.
Add Compose Action
- Add a "Compose" action to your flow.
- In the Inputs field, use the following expression
- convertTimeZone(utcNow(), 'UTC', 'India Standard Time', 'dd/M/yyyy h:mm:ss tt')
Add Inputs for Numerator and Denominator
- Click on + New step.
- Select Initialize variable.
- Name the variable Numerator, set the type to Integer, and provide a default value.
- Repeat the above steps to create another variable named Denominator.
![Variable]()
Add a Scope for Try Block
- Click on + New step.
- Search for Scope and add it.
- Rename the scope to Try.
- Inside the Try scope, add a Compose action.
- In the Inputs field, use the expression:
- div(variables('Numerator'), variables('Denominator'))
![Inputs]()
Add a Scope for Catch Block
- Click on + New step.
- Add another Scope and rename it to Catch.
Log Error in Excel
- Inside the Catch scope, add a Add a row into a table action.
- Rename to Add a row into a table - Failed.
- Select your SharePoint site and the document library where your Excel file is stored.
- Map the fields to log the error details (e.g., Flow Name, Trigger time).
- In the Inputs field, use the expression:
- Flow Name: workflow()?['tags']?['flowDisplayName']
- Trigger time: “Compose" action
![Compose action]()
Configure Run After Settings
- Click on the three dots (ellipsis) on the Catch scope.
- Select Configure to run after.
- Check the boxes for has failed and has timed out.
![Failed]()
Add a Scope for Finally Block
- Add another Scope and rename it to Finally.
- Inside the Finally scope, you can add the Compose action.
- Rename to Compose Flow run link.
- In the Inputs field, use the following expression to get the failed flow link
- concat('https://us.flow.microsoft.com/manage/environments/', workflow()?['tags']?['environmentName'],'/flows/',workflow()?['name'],'/runs/',workflow()?['run']['name'])
- add Get my profile (V2) action
![Compose flow run link]()
- add Send an email (V2) action to send flow failure mail
![V2]()
Add Adaptive cards in Power Automate
Adaptive cards in Power Automate are a powerful way to share and display information in a visually appealing and interactive format. These cards are created using JSON and can be integrated into various platforms, such as Microsoft Teams, to enhance communication and workflows.
Integration with Microsoft Teams: You can post adaptive cards to Teams channels or directly to users
Use Cases: Common use cases include sending notifications, collecting feedback, conducting polls, and more.
![]()
below is the JSON code for the Adaptive card
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Hello! Check your flow.",
"weight": "Bolder",
"size": "Medium"
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "Flow Run Link",
"url": "https://us.flow.microsoft.com/manage/environments" /* use flow run link here */
},
{
"type": "Action.Submit",
"title": "OK"
}
]
}
Below is the Adaptive card sent by Power Automate Using the above JSON code on Teams Channel
![ada2]()
Add a Terminate action.
As soon as the flow fails, add a row into a table action. Add (Flow Name, Trigger time). Into Excel below is a snap.
![Flow name]()
Send an email to the shared mail with the flow failure notification.
![Shared Mail]()
Conclusion
By implementing error handling in your Power Automate flows, you can ensure that your workflows are robust and reliable. Handling errors gracefully and recording error details in a SharePoint table helps you maintain data integrity and provides a clear audit trail for troubleshooting. Additionally, sending notifications to users ensures that any issues are promptly addressed.