Versions Compared

Key

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

...

Camel uses DefaultExceptionPolicyStrategy to determine a strategy how an exception being thrown should be handled by which onException clause. This The strategy is:

  • the order in which the onException is configured takes precedence. Camel will test from first...last defined.
  • camel Camel will start from the bottom (nested caused by) and recursive up in the exception hierarchy to find the first matching onException clause
  • instanceof test is used for test testing the given exception with the onException clause defined exception list. An exact instanceof match will always be used, otherwise the onException clause that has an exception that is the closets super of the thrown exception is selected (recurring up the exception hierarchy)

This is best illustrated with an exception:

Code Block
java
java
  onException(FileNotFoundExceptionIOException.class).handledmaximumRedeliveries(true).to("log:nofile")3);

  onException(OrderFailedException.class).maximumRedeliveries(2);

  onException(IOException.class).maximumRedeliveries(3);

In the sample above we have defined two exceptions in which FileNotFoundException IOException is first, so Camel will pickup this exception if there is a match. IOException that is more general is selected then.

...

Then Camel will try testing the exception in this order: FileNotFoundException, IOException, OrderFailedException and RuntimeCamelException.
As we have defined a onException(FileNotFoundException) Camel will have a match very early.select this as it's an exact match.

If we add a third onException clause with the FileNotFoundException

Code Block
java
java

  onException(IOException.class).maximumRedeliveries(3);

  onException(OrderFailedException.class).maximumRedeliveries(2);

  onException(FileNotFoundException.class).handled(true).to("log:nofile");

Then with the previous example Camel will now use the last onException(FileNotFoundException.class) as its an exact match. Since this is an exact match it will overrule the general IOException that was used before to handle the same exception thrown.

Now a new situation However if this exception was thrown instead:

...

Then the onException(OrderFailedException.class) will be selected - no surprise here.

And this last sample demonstrates the instanceof test aspect in which Camel will select an exception if it's an instance of the defined exception in the onException clause. Illustrated as:

...