I want to catch all Exception from routes.
I add this OnExeption :
onException(Exception.class).process(new MyFunctionFailureHandler()).stop();
Then, I create the class MyFunctionFailureHandler.
public class MyFunctionFailureHandler implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
Throwable caused;
caused = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
exchange.getContext().createProducerTemplate().send("mock:myerror", exchange);
}
}
Unfortunately, it doesn't work and I don't know why.
if there is an Exception, the program must stop.
How can I know why this code doesn't work!!
Thanks.
I used this on my route:
and in my errorProcessor
I hope it helps
keep in mind that if you have routes in multiple RouteBuilder classes, having an onExcpetion within this route will affect all routes under this RouteBuilder only
review this answer
additionally if an exception occurs within a route and handled inside this route,it won't be propagated to the original caller route you need to use noErrorHandler() i.e.
from(direct:start).errorHandler(noErrorHandler())
to propagate the exception handling back to the caller