Hi, I'm back again with try/catch and split. I have created the following unit test who seems to work but I don't understand why the try/catch block of the test does not receive the error ?
Code package org.apache.camel.processor; import org.apache.camel.ContextTestSupport; // import org.apache.camel.RuntimeCamelException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.impl.JndiRegistry; public class SplitterTryCatchErrorTest extends ContextTestSupport { public void testSplitWithError() throws Exception { MockEndpoint resultEndpoint = getMockEndpoint("mock:error"); resultEndpoint.expectedBodiesReceived("James"); try { template.sendBody("direct:startWithError", "cl...@james @Willem"); //fail("Should have thrown an exception"); } catch (Exception e) { assertTrue(e.getCause().getMessage().startsWith("This is a dummy error James!")); } assertMockEndpointsSatisfied(); } protected JndiRegistry createRegistry() throws Exception { JndiRegistry jndi = super.createRegistry(); jndi.bind("error", new GenerateError()); return jndi; } protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from("direct:startWithError") .split(body().tokenize("@")) .doTry() // GENERATE A DUMMY ERROR .to("bean:error") .to("mock:result") .doCatch(java.lang.Exception.class) .to("mock:error") .end(); } public static final class GenerateError { public GenerateError() { // Helper Class } public String dummyException(String payload) throws Exception { if (payload.equals("James")) { throw new Exception("This is a dummy error James!"); } return "Hello " + payload; } } } What I observe in the log is that the split of the body is done and each message is processed individually : one for Claus, one for Willem. For James, a dummy exception is throwed. 2009-08-12 14:41:06,701 [main ] DEBUG ProducerCache - >>>> Endpoint[direct://startWithError] Exchange[Message: cl...@james@Willem] 2009-08-12 14:41:06,811 [main ] DEBUG ProcessorEndpoint$1 - Starting producer: Producer[bean://error] 2009-08-12 14:41:06,811 [main ] DEBUG ProducerCache - Adding to producer cache with key: Endpoint[bean://error] for producer: Producer[bean://error] 2009-08-12 14:41:09,233 [main ] DEBUG BeanProcessor - Setting bean invocation result on the IN message: Hello Claus 2009-08-12 14:41:09,233 [main ] DEBUG BeanProcessor - Setting bean invocation result on the IN message: Hello Claus 2009-08-12 14:41:09,248 [main ] DEBUG MockEndpoint$1 - Starting producer: Producer[mock://result] 2009-08-12 14:41:09,248 [main ] DEBUG ProducerCache - Adding to producer cache with key: Endpoint[mock://result] for producer: Producer[mock://result] 2009-08-12 14:41:09,248 [main ] DEBUG MockEndpoint - mock://result >>>> 1 : Exchange[Message: Hello Claus] with body: Hello Claus 2009-08-12 14:41:09,248 [main ] DEBUG Pipeline - Message exchange has failed so breaking out of pipeline: Exchange[Message: James] Exception: java.lang.Exception: This is a dummy error James! 2009-08-12 14:41:09,264 [main ] DEBUG MockEndpoint$1 - Starting producer: Producer[mock://error] 2009-08-12 14:41:09,264 [main ] DEBUG ProducerCache - Adding to producer cache with key: Endpoint[mock://error] for producer: Producer[mock://error] 2009-08-12 14:41:09,264 [main ] DEBUG MockEndpoint - mock://error >>>> 1 : Exchange[Message: James] with body: James 2009-08-12 14:41:09,264 [main ] DEBUG TryProcessor - The exception is handled: true for the exception: java.lang.Exception caused by: This is a dummy error James! 2009-08-12 14:41:09,264 [main ] DEBUG BeanProcessor - Setting bean invocation result on the IN message: Hello Willem 2009-08-12 14:41:09,264 [main ] DEBUG BeanProcessor - Setting bean invocation result on the IN message: Hello Willem 2009-08-12 14:41:09,264 [main ] DEBUG MockEndpoint - mock://result >>>> 2 : Exchange[Message: Hello Willem] with body: Hello Willem 2009-08-12 14:41:09,264 [main ] DEBUG MulticastProcessor - Done sequientel processing 3 exchanges 2009-08-12 14:41:09,264 [main ] INFO MockEndpoint - Asserting: Endpoint[mock://result] is satisfied 2009-08-12 14:41:09,264 [main ] INFO MockEndpoint - Asserting: Endpoint[mock://error] is satisfied 2009-08-12 14:41:09,264 [main ] DEBUG SplitterTryCatchErrorTest - tearDown test: testSplitWithError The test succeeds but the exception is not catched in the try/catch block of testSplitWithError. What is wrong ? Regards, Charles