6

Is it possible to throw an exception in a Logic App?

Specifically, I'm trying to throw an exception inside of a scope. Then in the run after of the scope I'd check if it failed and inspect it for errors. I tried using a terminate inside of a scope, but that terminates the entire logic app run.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Joe Eng
  • 1,072
  • 2
  • 15
  • 30

4 Answers4

9

As an updated solution we can use Terminate control, Terminate control has 3 status: Failed, Canceled, and Succeeded.

enter image description here

Kamran
  • 1,258
  • 1
  • 16
  • 28
4

No, there is no Action or Connector directly analogous to something like a throw in C#.

The closest you can get right now would be to do something like use another LogicApp instead of a scope from which you can return a specific status code.

Johns-305
  • 10,908
  • 12
  • 21
  • What about using variables? Let's say an action failed within a scope and we assign the status code & error to variables (run after failure). We can then use these variables outside of the scope and take decission upon the values of those variables no? I agree that it can make the Logic App design more complex (and even more expensive), but in the end it's workable no? – Steven Van Eycken Mar 26 '18 at 13:25
  • 1
    Sure, that will totally work, the status variable is just another variable you check just like any other variable. Status checking is quite common for scenarios that also don't support 'traditional' exception handling. – Johns-305 Mar 26 '18 at 13:45
  • @StevenVanEycken yes, this is exactly how I ended up doing it. It works, but feels clunky. – Joe Eng Mar 26 '18 at 15:38
  • Could you use the Terminate action ... https://learn.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-actions-triggers#terminate-action – SteveC Aug 15 '19 at 08:50
4

Quick Answer (TL;DR)

  • Problem: MSFT Azure logic app throwing exceptions
  • Workaround: Use logic app Scope element to simulate throwing exceptions
    • Create a scope element to use as your "try-catch" block
    • Force the scope element to fail with an invalid command to simulate an exception
    • Allow the scope element to fail naturally and that will count as an exception too

Detailed Answer

Context

  • MSFT Azure logic app workflows
  • live version as of 2020-06-14

Problem

  • Scenario: Developer wishes to throw an exception or use try-catch semantics in a logic app

Solution

  • As of this writing, the live version of the product does not support this feature. There is a workaround.

Workaround

  • Use logic app scope element
  • add a conditional statement inside the scope element
  • If the conditional statement meets the failure condition, force an exception with a deliberately invalid command
    • for example create a variable assignment with the value int('__ERROR__')
  • If the conditional statement does not meet the failure condition, do nothing
  • The rest of the logic app consists of two paths
    • The first path runs on the success of the scope element
    • The second path runs on the failure of the scope element (failed, skipped, timed out)

Example

  • Create a scope element as a "try-catch" block

using scope element to simulate throwing an exception

  • Create a variable compose element with an invalid command invalid variable compose
    
    int('__ERROR__')  ## this will cause the enclosing scope to fail
                      ## the string __ERROR__ cannot be cast to integer
  • Respond to exit status of the Scope element enclosing your exception Respond to the exit status of the enclosing Scope

See also

dreftymac
  • 31,404
  • 26
  • 119
  • 182
  • 1
    Thanks for the detailed answer. I'd say this isn't ideal but probably the best we can do for now. – Joe Eng Jun 16 '20 at 04:09
  • 3
    I struggled to get this to work until I added the @ to the compose code in your screen shot ... "@int('__ERROR__')" – SteveC Dec 15 '20 at 12:37
  • @SteveC good point ... whenever working with logic app code you have to distinguish between `mycommand('foo')` and `@{mycommand('foo')}` because of the way JSON gets interpolated versus the way the code is composed in the logic app GUI – dreftymac Dec 19 '20 at 18:02
2

It seems like there still is no option for this inside Logic App or its little brother Power Automate / Microsoft Flow.

The way I have come up with and have used in some flows, is I simply add an action for something I know for a fact will fail.

The simplest (and probably the cheapest as the built-in actions cost less in Logic Apps, even if we are talking fractions of a dollar here either way) is probably to initialize a variable, e.g. called ThrowException with type of integer. Then a "Set variable" action is added wherever I want my flow to fail, where I set the value (remember it is of type integer) to any string expression. I simply use the expression string('Exception').

Simple example screenshot

Since the value is set via an expression this is still a valid template, but will fail upon runtime when the value is actually being set. After this, simply use parallel branches, with appropriate Run After settings, as usual.

  • **Note:** the example in the screenshot can be refactored to just one logicapp element instead of two. This can be done by using the `variable compose` element with a logicapp expression that is guaranteed to fail. For example you can use `int('_error_')`. – dreftymac Jun 25 '20 at 12:41