onException() is the Apache Camel Java DSL clause that defines what happens when a route throws a specific exception type. Instead of letting the exception propagate to the caller, you can retry the message, mark the failure as handled, route the message to an error endpoint, or return a custom response, and you can do this per exception class.
It is the main tool for fault tolerance in Camel routes. This article uses Apache Camel 4 (Jakarta namespace, Java 17 or newer) with Spring Boot, but the DSL is the same in recent Camel 3 releases.
Every Camel route runs inside an error handler. The default one logs the exception and fails the exchange. onException() lets you declare exception policies that the error handler consults when processing fails. Each policy can:
Policies are evaluated when an exception is set on the exchange, not at route start. The route steps defined inside the onException block run only on failure.
With handled(true), Camel treats the exception as dealt with: the caller gets the body and headers produced by the onException block and no exception is thrown. The original exception is still available in the exchange property Exchange.EXCEPTION_CAUGHT, and in the Simple language as ${exception.message} or ${exception.stacktrace}.
Transient failures such as a timeout call for retries before giving up:
Camel retries the failed step up to four times, waiting roughly 1, 2, 4 and 8 seconds (the exponential backoff multiplier doubles the delay). If all attempts fail, the exchange is handled and sent to orders.failed. Redelivery restarts at the failed processor, not at the beginning of the route, unless you enable useOriginalMessage() to route the original input message instead of the modified one.
The three outcomes are easy to confuse:
| Setting | Effect on the route | Caller sees |
|---|---|---|
handled(true) | Stops the route, runs the onException steps, ends normally | No exception |
continued(true) | Ignores the failure and resumes at the step after the failing one | No exception |
handled(false) (default) | Runs the onException steps, then the exception continues | The exception |
Use continued(true) only for non-critical steps, such as optional enrichment, where it is acceptable to carry on with partial data.
An onException placed directly in configure() applies to all routes of that RouteBuilder class. To restrict it to one route, define it inside the route:
The route-scoped policy takes precedence over a builder-level one for the same exception type. Always close a route-level block with .end(), otherwise the following steps become part of the exception handler.
You can narrow a policy with onWhen(), for example to handle only HTTP 503 responses:
For a catch-all strategy, configure the route-wide error handler with a dead letter channel, and keep onException for the cases that need special treatment:
doTry()...doCatch() is a local try/catch around a few steps in a single route. onException() is a declarative policy that applies across routes and supports redelivery. Prefer onException for cross-cutting error handling and retries, and doTry when the handling is specific to one block and the flow should continue immediately after it.
It marks the exception as handled so the route stops, the onException steps run, and the caller receives the resulting message instead of an exception.
It applies to all routes defined in the same RouteBuilder class. For a different scope, define it inside one route, or declare it in a shared base builder that your routes extend.
List those exception classes in onException() and set maximumRedeliveries(). Exceptions with no matching policy are handled by the route's error handler, which by default does not retry.
Use ${exception.message} in the Simple language, or exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class) in a processor.
onException() declares how Camel reacts to specific failures: retry with backoff, handle and respond, continue after the failing step, or route to an error endpoint. Choose handled(true) to swallow and answer, scope policies globally or per route, and combine them with a dead letter channel for anything unexpected.