MS Power Automate : Exception handling with Try-Catch-Finally

What is Issue?

MS providing all new ways to develop the application and moving to the less-code implementations. For implementing quick asynchronous logics, MS introduced Power Automate for more than hundred of the products. With traditional coding, developer knows how to implement exception handling. But what about power automate?

The Reason-

Every complex logic is exception prone and implementing it with traditional coding way, it is easy to handle with try-catch-finally in code. We need to understand the exception handling in Power Automate.

Solution-

Power Automate provides many actions and configurations, which we can use to implement Try-Catch-Finally. Lets see-

Example- I am using Account field to check how many times account name is updated. Every time when account is updated, need to send the email notification, in both the cases when name update is failed or success.

We need 3 control -> Scope, each with name – Try, Catch and Finally

Add business logic into the Try block. Variable initialization is not allowed in the Scope control, so you need to initialize all variables at the top of the Try block only.

Configure Catch block to run after “Try” only if “Try” block Failed, or Timed-out.

Configure Finally block to run after “Catch” if “Catch” block Failed, Successful, Timed-out or Skipped

Add logic in Catch block – how to read the exact errors and send it to any user/Team or stored in any record, if some thing fails in Try block?

Use the result from Try block and filter it with only failed or Timed out actions-so use the Filtered Array and iterate through each item from filtered array-

Some useful expressions –

Filtered array From result of Try :

result('Try')

Filter only failed and timed out results using advanced mode in Filtered array –

@contains(createArray('Failed', 'TimedOut'), item()['status'])

Read action name which is failed-

@{items('Apply_to_each_result')['name']}

Read Status-

@{items('Apply_to_each_result')['status']}

Read exact error Message-

@{items('Apply_to_each_result')['error/message']}

Set the Subject line and body in the Try and Catch both, so that in Finally we can send the success or failure email. Now, my Try-Catch-Finally blocks looking like this->

Now testing Time-

On successful run, it will skip Catch Block but will hit the Finally->

Email received on successful flow run-

On Error in Try, it will hit the Catch block and capture the failure details. It will hit Finally to send email on failure with error details-

Email received on failed flow run-

In failure- You can add the current flow run log url, so that any developer can easily track the log and find out the exact issues for failure.

We can use the below syntax to get current flow run log url-

concat('https://us.flow.microsoft.com/manage/environments/', workflow()?['tags']['environmentName'], '/flows/', workflow()?['name'], '/runs/', workflow()?['run']['name'])

That’s it!! You learnt the Exception handling in MS Power Automate.

Hope this will help…
Enjoy Ms CRM!!!

Follow on Facebook- FB: MSCRM16Tech

Leave a comment