I have a route with doTry() - doCatch() pair for a specific route and onException() in general.
onException(Exception.class)
    .handled(true)
    .log(LoggingLevel.ERROR, "An error occurred: ${exception.stacktrace}")
    .setBody(simple("${exception}"))
    .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500));
from("direct:mydirect")
        .routeId("myRoute")
        .doTry()
           .to("direct:internalroute")
        .doCatch(Exception.class)
            .log(LoggingLevel.ERROR, "EXCEPTION: ${exception.stacktrace}")
            .process(exceptionHandlerProcessor)
            .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500))
            .marshal(new JsonDataFormat(JsonLibrary.Jackson))
        .doFinally()
            .log("FINALLY")
        .endDoTry();
Internal route throws a plain java.lang.Exception
 throw new Exception("Catch me if you can!");
I expected the exception to be caught in doCatch() and logging and pocessing operations to be executed. However, onException() is invoked instead.
Does onException() have a higher prority? In my understanding local catch is more prioritized.
P.S. Removing onException() makes doCatch() invoked. However I have reasons to keep both. Camel version is: org.apache.camel:camel-cxf:2.21.0.000033-fuse-000001-redhat-1
When you have a doTry .. doCatch block and you call another route, such as you do via
.to("direct:internalroute")
Then you need to turn off error handling on that route, eg in
from("direct:internalroute")
  .errorHandler(noErrorHandler())
If you want all error handling to happen via the doTry .. doCatch block only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With