Page Edited :
CAMEL :
Try Catch Finally
Try Catch Finally has been edited by Claus Ibsen (Apr 20, 2009). Content:Try ... Catch ... FinallyCamel supports the Java equivalent of try .. catch and finally directly in the DSL. In Camel we prefix the keywords with do to avoid having same keyword as Java. So we have:
Notice this document is based on how it works in Camel 2.0. In Camel 1.x this feature isn't as powerful and it uses a slight different keyword names. About doCatch and its power over JavaThe doCatch in Camel is empowered over its Java sister. First of all you can define multiple exceptions to catch in a single block. And secondly you can attach a onWhen predicate to signal if the catch should trigger or not at runtime. To simulate rehrowing an exception from a doCatch you should use the handled predicate. If its evaluated to false Camel will reattach the exception on the Exchange. And just like Java the order in which you have multiple doCatch blocks matter. Camel will iterate from the top going down and use the first doCatch that matches the exception and if the onWhen predicate matches as well (if any provided). This is the same behavior as the Exception Clause. Using try .. catch .. finally in Java DSLIn the route below we have all keywords in action. As the code is based on a unit test we route using Mock. And in the route below we want to indicate if an IOException occured we want to route it elsewhere and at the same time keep the exception so the original caller is notified about this exception. To do this we need to not rethrow the exception and this is why we use handled and set it to false to indicate, no we did not handle it so please keep the exception. from("direct:start") // here is our try where we try processing the exchange in the route below if it fails // we can catch it below, just like regular try .. catch .. finally in Java .doTry() .process(new ProcessorFail()) .to("mock:result") // catch IOExcption that we do not want to handle, eg the caller should get the error back .doCatch(IOException.class) // mark this as NOT handled, eg the caller will also get the exception .handled(false) .to("mock:io") .doCatch(Exception.class) // and catch all other exceptions // they are handled by default (ie handled = true) .to("mock:error") // here the try block ends .end();
|
Unsubscribe or edit your notifications preferences