Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 second of all an important aspect over the regular Java counter parts is that Camel will check in the exception hierarchy when it matches a thrown exception against the doCatch blocks. The reasons is that many times the original caused exceptions is wrapped by other wrapper exceptions, typically transposing the exception from a checked to a runtime exception.
Camel for instance does this by wrapped it in a CamelRuntimeException. So if the original caused exception is an java.io.IOException then Camel will still match a doCatch block defined with an java.io.IOException. 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. The reason is to keep it similar to the regular java and how it selects a catch block. This differers from the Exception Clause that has a more intelligent exception selection strategy among multiple onException definitions, where it also consider the delta in the exception hierarchy to select the best definition.

A third feature is that you can attach a onWhen predicate to signal if the catch should trigger or not at runtime.

And 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.

Using try .. catch .. finally in Java DSL

...