Babel Camel Error management

Babel provides mainly two ways to deal with errors : depending on if the handling is Exception specific or not. In any case, the error handling is done using the handle keyword.

Handle keyword

Error handling handle can handle one or several error-handling


The error handling keywords, when defined in a route, are member of a set of keywords which should follow directly the from keyword or any keyword of this set.

General error handling

This part concerns error handling which does not take into account the type of the raised exception. It just let you define strategies that are used for any raised exception:

general error-handling

the four error-handling policy we will have a look at and their configuration.

DeadLetter channel

The deadletter will send received exception to the defined endpoint.

  //Message causing exception would be sent to the deadletter


The LoggingErrorHandler will just received exception to the defined logger at the defined logging level.

import org.apache.camel.LoggingLevel
import org.slf4j.LoggerFactory

  //logs raised Exception at the Trace level
  .handle(_.loggingErrorHandler(level = LoggingLevel.TRACE,
    logger = LoggerFactory.getLogger("")))

  .processBody(_ => throw new Exception())


The default error handler is, as its name claims, implicit. This may be used to override inherited error handler.

  //The exchange may get redelivered twice before the Exception is raised higher


The NoErrorHandler overrides errorhandling defined by a route which is given as input of the current route.

val masterRoutes = new RouteBuilder {
val errorRoutes = new RouteBuilder {
    //erase the error handling policy defined in the higher route
    .process(any => throw new IllegalArgumentException(s"blah:$any")).

Exception clauses

You may define specific error management depending on the type of the thrown exception using the on keyword:

  • when the exception should be treated, which is the parameter of the on keyword
  • continued
  • handled
OnException clauses


The when parameter allows you to be more precise about the exception which should be process through this exception clause. In the below example, the exceptions which message contains “toto” are received in the output. The exceptions which message contains “tata” are handled but would not reach the output.

  .handle {
    route =>
      //Message containing "toto" and causing an Exception should continue the route
      route.onBody[Exception]((x: Any) => x.toString.contains("toto"))
      //Message containing "tata" and causing an Exception should stop and
      //    the Exceptionshould be tagged as handled
      route.onBody[Exception]((x: Any) => x.toString.contains("tata"))


The continued keyword allows to specify if the exchange should continue processing within the original route. The continued keyword accepts a Camel Predicate or a Boolean function.

  .handle {
    route =>
      //Messages which causes IllegalArgumentException
        //are catch and continue the flow if body is "toto"
        continuedBody((x: Any) => x.toString == "toto")


The handled keyword accepts a Camel Predicate or a Boolean function. If the parameter to handled keyword evaluates to true then the exception will not be raised with a caller:

  .handle {
    route =>
        .handledBody((x: Any) => x.toString.contains("toto"))


Handling errors

You may use the handlingRoute keyword to define an endpoint which should receive this exception

  .handle {
    //Any message which cases an IllegalArgumentException
      //should be transfered, via another route called consuming from "direct:exception"

Configure several routes

Error handling for Routes and RouteBuilder

Every Error handling keyword may also be used for every route defined in the RouteBuilder by using the handle keyword as if it was the beginning of a route:

handle {
  route =>
    //Message raising Exception in any of the following route
    //    will be sent to the deadletter

In the example above, the two routes send their exceptions to the mock:error endpoint.