Try ... Catch ... Finally
Camel supports the Java equivalent of try .. catch and finally directly in the DSL.
It aims to work like its Java sisters but with more power. Especially in Camel 2.0 where we gave this feature an overhaul.
In Camel we prefix the keywords with do
to avoid having same keyword as Java. So we have:
doTry
doCatch
doFinally
end
to end the block in Java DSL
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 Java
The 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.