Access Keys:
Skip to content (Access Key - 0)
Cancel    
Cancel   
 

Contents

Catch Exception Strategy

You can define a catch exception strategy to customize the way Mule handles messages with errors. A catch exception strategy catches all exceptions thrown within its parent flow and processes them, thereby overriding Mule’s implicit default exception strategy. (Mule’s catch exception strategy behavior is similar to a Java catch block.)

 Contents

When to Use

Use a catch exception strategy to design a unique strategy for handling a message that contains an error. Further, you can use a catch exception strategy to:

  • avoid propagating exceptions to inbound endpoints
  • avoid propagating exceptions to parent flows via Flow Reference Components
  • ensure that a transaction processed by the flow will not be rolled back when an error occurs (i.e. 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.

     

    Configuring a Catch Exception Strategy

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



      2. Double-click to open the Catch Exception Strategy, then configure according to the table below.

        FieldValue
        Display NameUnique name for the exception strategy, if you wish.
        Execute When

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

        no expression defined: All messages in this flow that throw exceptions will be 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 which throw an org.mule.example.AlreadyProcessedException.

         
         

        Enable Notificationstrue (default)
        false  
        When set to true, Mule to send an exception notification to a registered listener — say, the Mule Management Console — whenever the catch exception strategy accepts handles an exception.

        What follows are some examples of expressions that you can enter in the Execute When field:

        • exception.causedBy(org.mule.example.ExceptionType)
        • exception.causedExactlyBy(org.mule.example.ExceptionType)
        • exception.causeMatches(org.mule.example.*)
        • exception.causeMatches(*) && !exception.causedBy(java.lang.ArithmeticException) && !exception.causedBy(org.mule.api.registry.ResolverException)
      3. Click OK to save your configurations.

      4. Drag building blocks 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.

      1. To your flow, below all the message processors, add a catch-exception-strategy element. Refer to code below.
      2. Configure attributes of the exception strategy according to the table below.

        AttributeValue
        doc:nameUnique name for the exception strategy, if you wish. (Not required in Standalone.)
        whenA MEL expression to indicate the kind of exception the catch exception strategy handles when it is embedded within a Choice Exception Strategy.

        no expression defined: All messages in this flow that throw exceptions will be 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 which throw an org.mule.example.AlreadyProcessedException.
        enableNotificationstrue or false
        When set to true, Mule to send an exception notification to a registered listener — say, the Mule Management Console — whenever the catch exception strategy accepts handles an exception.

        What follows are some examples of expressions that you can use as values of the when attribute:

        • exception.causedBy(org.mule.example.ExceptionType)
        • exception.causedExactlyBy(org.mule.example.ExceptionType)
        • exception.causeMatches(org.mule.example.*)
        • exception.causeMatches(*) && !exception.causedBy(java.lang.ArithmeticException) && !exception.causedBy(org.mule.api.registry.ResolverException)
         View the Namespace
      3. 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 global catch exception strategy, then add a Reference Exception Strategy to a flow to apply the error handling behavior of your new global catch exception strategy.

        1. In the Global Elements tab in Studio, create a Global Catch Exception Strategy (below, left), configure it according to the table below (refer to image below, right), then click OK to save. 


          FieldValue
          Display NameUnique name for the exception strategy, if you wish.
          Execute When

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

          no expression defined: All messages in this flow that throw exceptions will be 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 which throw an org.mule.example.AlreadyProcessedException.

          Enable Notificationstrue (default)
          false  
          When set to true, Mule to send an exception notification to a registered listener — say, the Mule Management Console — whenever the catch exception strategy accepts handles an exception.
        2. Click on the Message Flow tab below the canvas. On the Message Flow canvas, note that your newly created global catch exception strategy box appears outside all other flows in the application. Because it is global, your new catch exception strategy exists independently of any Mule flow. 



        3. Drag building blocks from the palette into the global catch exception strategy box to build a flow that processes messages that throw exceptions. A global catch exception strategy can contain any number of message processors.

        1. Above all the flows in your application, create a catch-exception-strategy element. 
        2. To this global catch-exception-strategy element, add the attributes according to the table below. Refer to code sample below.

          AttributeValue
          nameUnique name for the exception strategy, if you wish.
          whenA MEL expression to indicate the kind of exception the catch exception strategy handles when it is embedded within a Choice Exception Strategy.

          no expression defined: All messages in this flow that throw exceptions will be 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 which throw an org.mule.example.AlreadyProcessedException.
          enableNotificationstrue or false
          When set to true, Mule to send an exception notification to a registered listener — say, the Mule Management Console — whenever the catch exception strategy accepts handles an exception.
           View the Namespace
        3. 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.

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



          2. Double-click to open the Reference Exception Strategy, use the drop-down to reference the global catch exception strategy (below), then click OK 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.

          You can create a global catch exception strategy (i.e. access the Choose Global Type panel) from the reference exception strategy’s pattern properties panel. Click on the (plus) button next to the Global Exception Strategy drop-down combo box and follow the steps above to create a global catch exception strategy.

           

          1. To your flow, below all the message processors, add an exception-strategy element.
          2. To the exception-strategy element, add attributes according to the table below. Refer to code below.

            AttributeValue
            refName of the global catch-exception-strategy in your project.
            doc:nameUnique 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.

           

          Go Further