How to do Error handling similar to try/ catch in Power Apps Canvas apps

How do I perform error handling in my Canvas Apps in Power Apps? This is a question I face in almost every training I conduct in Power Apps.

After all why not? Isn’t it the most simplest of requirement to catch an error in an application if something goes wrong so that end-users don’t face the ugly error message.

As easy it may sound and quite often we take for granted in so many other platforms, it was not quite that easy in Canvas apps. However to much of our relief, Microsoft have finally introduced functions In PowerFx. They are

  • IfError
  • IsError
  • IsBlankOrError

You can find the pre-release documentation here.

Before you go ahead, a word of caution. As of the time of writing this blog, it is a pre-release documentation and subjected to change. However my personal take on this – It’s something utmost essential and I don’t see a way back from here. It’s only going to improve.

So let’s see the IfError function in action. Let’s take a very very simple scenario to start with.

Error handling using Try catch in Canvas app

In the screenshot above, I am dividing by zero. Quite obvious it shall throw an error and here in the function IfError comes in handy.

Below is the output when I click on the “Call Flow” button.

Error handling using Try catch in Canvas app

Please note: IfError is an experimental feature and you need to enable the same. To enable this feature navigate to Settings -> Upcoming features -> Experimental -> Formula-level error management and set this to on.

Error handling using Try catch in Canvas app

Well that was a very easy one. Let’s take a little more complex scenario. Let’s run a patch statement against DataVerse. In this way we shall learn on how to handle errors if operations on DataVerse or for that any other data source from canvas app throw an error.

I change the code on click of the button to below.

Error handling using Try catch in Canvas app

I have created a business rule which shall stop any creating any account where name contains test. So this patch statement to create a record shall fail. And the notify statement shall show up.

One more important point to note here is that IfError works in sequence. In the above code, if the first patch statement succeed then the second path statement shall execute. If the second patch statement fail, then the fallback operation which in our case is Notify operation, executes.

Let’s go one level up in error handling. Frequently we need to invoke Power Automate flow from canvas apps. What if the flow operation fails? How do we handle errors from Power Automate when invoked from Canvas apps? Let’s check together.

Error handling using Try catch in Canvas app

Above is a Power Automate flow where I have forcibly introduced the Divide by Zero error. And this Power Automate flow I am going to invoke from my canvas app.

One thing to remember here. If you want an error to pass the error back to the flow, you need to use the Response action to pass the output of action back to the canvas app.

Error handling using Try catch in Canvas app

And you should be able to successfully handle error message.

Hope that helped!

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading