Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: CAMEL-1144 explained how exception policy strategy works with samples

...

Here if the processing of seda:inputA or seda:inputB cause a ValidationException to be thrown (such as due to the XSD validation of the Validation component or the Relax NG Compact syntax validation of the Jing component), then the message will be sent to activemq:validationFailed queue.

Exception policy

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

  • the order in which the onException is configured takes precedence. Camel will test from first .. last defined.
  • 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 the given exception with the onException clause defined exception list

This is best illustrated with an exception:

Code Block
java
java

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

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

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

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

So if an exception is thrown with this hierarchy:

Code Block

+ RuntimeCamelException (wrapper exception by Camel)
   + OrderFailedException 
       + IOException
            + FileNotFoundException

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.

However if this exception was thrown instead:

Code Block

+ RuntimeCamelException (wrapper exception by Camel)
   + OrderFailedException 
       + OrderNotFoundException

Then the onException(OrderFailedException.class) will be selected.

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:

Code Block

+ RuntimeCamelException (wrapper exception by Camel)
   + SocketException

Since SocketException is an instanceof IOException, Camel will select the onException(IOException.class) clause.

Catching multiple exceptions

...