Ensure that a transaction processed by the flow is not rolled back when an error occurs (that is, the transaction is never “rolled back” to reattempt processing; Mule commits the transaction.)

For example, suppose you have a flow that processes messages from a JMS queue (a reservation for a flight), enriches them by adding a property acquired from an external resource (seat assignment), and then sends them to another queue. You configure a catch exception strategy to handle errors that occur in this flow; when an error occurs during processing — say the seat information is not available from the external resource — the message throws an exception. The catch exception strategy catches the exception, applies a header to it to advise that “no seats are available,” and pushes the message out of the flow to its next designated queue.

Drag processors from the palette into the Catch Exception Strategy box to build a flow that processes messages that throw exceptions in the parent flow. A catch exception strategy can contain any number of message processors.

You can define only one exception strategy for each flow. If you need to design a more complex error handling strategy that involves more than one way of handling exceptions, consider using a Choice Exception Strategy.

To your flow, below all the message processors, add a catch-exception-strategy element. Refer to code below.

Configure attributes of the exception strategy according to the table below.

Attribute

Value

doc:name

Unique name for the exception strategy, if you wish. (Not required in Standalone.)

when

A MEL expression to indicate the kind of exception the catch exception strategy handles when it is embedded within a Choice Exception Strategy.

The following states exist for the when parameter:

No expression defined: All messages in this flow that throw exceptions are handled by the catch exception strategy.

Expression defined: If the exception strategy evaluates the defined expression against the message being processed and returns true, Mule executes the exception strategy.

For example, if you enter the following expression, the exception strategy handles only those messages that throw an org.mule.example.AlreadyProcessedException.

#[exception.causedBy(org.mule.example.AlreadyProcessedException)]

enableNotifications

true or false

When set to true, Mule sends an exception notification to a registered listener — say, the Mule Management Console — whenever the catch exception strategy accepts handles an exception.

The following are sample MEL expressions you can enter in the when attribute:

Add message processors as child elements of the catch-exception-strategy to build a flow that processes messages that throw exceptions in the parent flow. A catch exception strategy can contain any number of message processors. Refer to sample code below in which a set-property and jms:outbound-endbpoint process exceptions.

You can define only one exception strategy for each flow. If you need to design a more complex error handling strategy that involves more than one way of handling exceptions, consider using a Choice Exception Strategy.

Creating a Global Catch Exception Strategy

You can create one or more global exception strategies to reuse in flows throughout your entire Mule application. First, create a Mule Configuration File and add to it a catch exception strategy. Add a Reference Exception Strategy to a flow to apply the error handling behavior of your new global catch exception strategy.

Before creating a Global Catch Exception Strategy, you must have first created a catch exception strategy as described in Configuring a Catch Exception Strategy. The global version of the exception strategy requires copying text from your current exception strategy to create the global file.

Click File > New > Mule Configuration File. You can use this configuration file to store the processors to share with all the flows in your project. This file appears in your Studio project under src/main/app. For this example, it’s called global.xml. The configuration file has the same elements the same as a Mule project so you can search for and drag processors into the configuration file.

Click Message Flow and copy the processors you want in the configuration file. The catch exception strategy should be in the configuration file.

For example, if the original flow contained a Catch Exception Strategy and an HTTP connector, add these elements to the configuration file.

Above all the flows in your application, create a catch-exception-strategy element.

To this global catch-exception-strategy element, add the attributes according to the table below. Refer to the code sample below.

Attribute

Value

name

Unique name for the exception strategy, if you wish.

when

A MEL expression to indicate the kind of exception the catch exception strategy handles when it is embedded within a Choice Exception Strategy.

The following states exist for the when parameter:

* No expression defined: All messages in this flow that throw exceptions are handled by this catch exception strategy. * Expression defined: When a choice exception strategy evaluates the defined expression against the message being processed and returns true, Mule executes the exception strategy.

For example, if you enter the following expression, the exception strategy handles only those messages that throw an org.mule.example.AlreadyProcessedException.

true or false - When set to true, Mule sends an exception notification to a registered listener such as the Mule Management Console whenever the catch exception strategy handles an exception. Default is true.

logException

true or false - When set to true, determines whether to log the handled exception to its standard logger in the ERROR level before being handled. Default is true.

Add message processors as child elements of the catch-exception-strategy to build a flow that processes messages that throw exceptions in the parent flow. A catch exception strategy can contain any number of message processors. Refer to sample code below in which a simple logger processes exceptions.

Applying a Global Catch Exception Strategy to a Flow

Use a reference exception strategy to instruct a flow to employ the error handling behavior defined by your global catch exception strategy. In other words, you must ask your flow to refer to the global catch exception strategy for instructions on how to handle errors.

From the Error Handling palette group, drag and drop the Reference Exception Strategy icon into the footer bar of a flow.

Open the Reference Exception Strategy’s Properties Editor, use the drop-down to reference the global catch exception strategy (below); click anywhere on the canvas to save.

You can append a Reference Exception Strategy to any number of flows in your Mule application and instruct them to refer to any of the global catch, rollback, or choice exception strategies you have created. You can direct any number of reference exception strategies to refer to the same global exception strategy.

To your flow, below all the message processors, add an exception-strategy element.

To the exception-strategy element, add attributes according to the table below. Refer to code below.

Unique name for the exception strategy, if you wish. (Not required in Standalone.)

You can append a Reference Exception Strategy to any number of flows in your Mule application and instruct them to refer to any of the global catch, rollback or choice exception strategies you have created. You can direct any number of reference exception strategies to refer to the same global exception strategy.