How to handle an exception in an exception block in apache camel

768 Views Asked by At

I am trying to handle an exception within apache camel in onException. Can someone guide me if it is possible?

I have written another onException which will handle all Exceptions, but the flow is not transferred to that exception block

onException(SchemaValidationException.class)
        .to("xslt:stylesheet/example/TransformErrorBlock.xsl?saxon=true")
        .log("Validation error in received message, response sent: ${body}")
        .handled(true);

My expectation is if there is an exception in this block, it should be caught in another onException block

2

There are 2 best solutions below

2
Claus Ibsen On

You cannot do this as its by design that Camel only allows onException block to handle exceptions, otherwise you can end up with endless looping when onException A is handled by onException which causes a new exception that may then be handled by onException A again, and so endless looping in circles.

0
Rajveer Singh On

This worked for me:

onException(SalesforceException.class)
    .handled(true)
    .log("mai hua")
    .process((exchange) -> {
        Exception e = new Exception("Some Exception");
        exchange.setProperty("CustomException", e);
    })
    .to("direct:exception");


onException(Exception.class)
    .handled(true)
    .log("mai bhi hua2");

from("direct:exception")
    .log("mai bhi hua in route")
    .process(exchange -> {
        throw exchange.getProperty("CustomException", Exception.class);
    });