A catch exception strategy can be defined to customize the way Mule handles messages with errors. A catch exception strategy catches all exceptions thrown within its 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, except that a new exception cannot be thrown or another exception cannot be caught within a catch exception strategy.
In my previous tutorials unlike local catch-exception-strategy and global catch-exception-strategy, the default catch exception strategy is configured by defining a defaultException-Strategy-ref attribute on the Mule configuration element and pointing it to the desired global exception strategy, which is configured like a regular global endpoint. When you need to configure an exception strategy for all (or most) of the flows, then you need default exception strategy. Defining the default exception strategy on the configuration element will cause all flows in the Mule application with no specific exception strategy to use the defined exception strategy.
Let’s start by configuring an exception strategy inside a flow. Let’s use a catch-exception-strategy that will catch and process all exceptions thrown in the flows
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd"> <spring:beans> <spring:bean name="priceCalculator" class="com.roytuts.mule.component.PriceCalculator" /> </spring:beans> <catch-exception-strategy name="defaultCatchStrategy"> <set-payload value="Error: #[exception.summaryMessage]" doc:name="Set Payload" /> </catch-exception-strategy> <configuration defaultExceptionStrategy-ref="defaultCatchStrategy" /> <flow name="default-catch-exception-strategy-flow" doc:name="default-catch-exception-strategy-flow"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8084" doc:name="HTTP" /> <invoke object-ref="priceCalculator" method="calculatePriceForItems" doc:name="Invoke" methodArgumentTypes="java.lang.Integer,java.lang.Double" methodArguments="#[message.inboundProperties.'http.query.params'.items],#[message.inboundProperties.'http.query.params'.cost]" /> </flow> </mule>
The graphical representation is shown below
The PriceCalculator java class source code
package com.roytuts.mule.component; public class PriceCalculator { final double taxRate = 0.5; public String calculatePriceForItems(Integer noOfItems, Double eachItemCost) { return "Total Cost : Rs. " + noOfItems * eachItemCost * taxRate + "/-"; } }
When you run the mule application, you will see below outputs on different inputs
Console Output
ERROR 2016-11-06 07:39:37,863 [[mule-3].connector.http.mule.default.receiver.05] org.mule.exception.CatchMessagingExceptionStrategy: ******************************************************************************** Message : For input string: "300s" (java.lang.NumberFormatException). Message payload is of type: String Code : MULE_ERROR--2 -------------------------------------------------------------------------------- Exception stack is: 1. For input string: "300s" (java.lang.NumberFormatException) sun.misc.FloatingDecimal:-1 (null) 2. For input string: "300s" (java.lang.NumberFormatException). Message payload is of type: String (org.mule.api.MessagingException) org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MessagingException.html) -------------------------------------------------------------------------------- Root Exception stack trace: java.lang.NumberFormatException: For input string: "300s" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at sun.misc.FloatingDecimal.parseDouble(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything) ********************************************************************************
Thanks for reading.