22

What's the difference between:

<camel:errorHandler id="deadLetterErrorHandler" type="DeadLetterChannel" 
        deadLetterUri="log:dead">

<camel:camelContext errorHandlerRef="deadLetterErrorHandler">
    ...
</camel:camelContext>

And:

<onException>
    ...
</onException>

According to this article, using them both in conjunction is a "powerful combination". How so? What roles do they each individual assume, and how do they complement each other?

2 Answers2

27

The errorHandler is used to handle any uncaught Exception that gets thrown during the routing and processing of a message. Conversely, onException is used to handle specific Exception types when they are thrown. Check out this article to see how to use onException.

Keith
  • 518
  • 1
  • 6
  • 10
  • 10
    So if I write `onException(Throwable.class)` I've basically implemented the `errorHandler`? – DavidS Aug 02 '17 at 17:49
  • I'm new to camel but as for me it lacks support for per-processor error handler - handler that will catch any exception. Sample code: ~~~
    
    from("direct:a") 
      .unmarshall(new MyDataFormat("param1"))
        .perProcessorErrorHandler(deadLetterQueue("log:foo"))
      .process(myProcessor)
        .perProcessorErrorHandler(deadLetterQueue("log:bar"))
      .to(myAnotherProcessor)
        .perProcessorErrorHandler(deadLetterQueue("log:baz"))
    ~~~
    this allows me not to think about types of exceptions for each processor
    – pls Sep 02 '22 at 06:35
4

If the action you need to perform for each type of exception is different, use onException. It lets you define error handling on a per exception basis.

onException(xxxException.class).to("activemq:xxxFailed"); onException(yyyException.class).to("activemq:yyyFailed");

If you just need a generic handler, go with errorHandler. For all type of errors, the same processing will be performed.

SMS Krishnan
  • 61
  • 1
  • 2