1

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

minie
  • 21
  • 3
  • is that `onException` defined above your route? As far as I remember on RouteBuilder-level it must be defined before the route. Alternatively try to define the on exception on route-level (at least to test the behaviour) like it is described [here](https://stackoverflow.com/a/32563467/7917574). - EDIT: that linked post confirms that the routebuilder level exception handler must be defined before the route – metters Jun 12 '19 at 12:18
  • 2
    OP is expecting Exceptions in their `OnException` block to be plumbed to another catch-all `OnException` clause in the same `RouteBuilder`. It won't work, as confirmed by @Claus Ibsen below. – ShellDragon Jun 12 '19 at 12:24
  • onException(SchemaValidationException.class).doTry() .to("xslt:stylesheet/example/TransformErrorBlock.xsl?saxon=true") .log("Validation error in received message, response sent: ${body}").doCatch(Exception.class).log("Unable to process input file") .handled(true); I have done something like this, but does not seem like an elegant way to handle it – minie Jun 12 '19 at 12:40
  • Thanks, I misunderstood the issue. – metters Jun 12 '19 at 13:01

2 Answers2

2

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.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • yeah the easy ones you answer but not my question: https://stackoverflow.com/questions/56462217/import-module-into-xquery-with-apache-camel-transformer-eip :-) just kiddin' ;-). Btw: Your book is very good – the hand of NOD Jun 12 '19 at 12:24
  • Sorry - I'm not sure what, then, is the best practise in dealing with an exception that is raised in an onException block? in my Route where I am using a Transacted AMQ client, I can see that the AMQ redelivery/DLQ policy kicks in, but I was wondering if there a way to send these exceptions straight to a DLQ or other error queue? – n99 Oct 16 '20 at 10:38
0

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);
    });
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83