Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Apache Camel’s Java DSL, use onException to define exception-specific behavior within Camel’s normal error-handling flow; use doTry/doCatch when you want a local try/catch block that acts as its own error handler. The route’s error handler supplies the broader failure strategy, while an exception clause can decide whether a particular failure is retried, handled, or allowed to continue.

How do I handle exceptions in a Camel Java DSL route?

Choose the construct based on how broadly the failure policy should apply and what should happen to the route after the failure:

Construct Scope and role Failure flow
onException Exception-specific policy, configured at RouteBuilder scope or for an individual route. Can configure redelivery, handle the exception and stop the failed route, or suppress it and resume the original route.
Error handler The broader strategy governing route-processing failures. Depends on the selected strategy; for example, the Default Error Handler propagates exceptions to the caller, while a Dead Letter Channel supports dead-letter routing.
doTry/doCatch/doFinally A local block inside a route. Provides local try/catch/finally-style control flow; Camel’s normal error handler and onException policies do not apply within the block.

Camel encourages combining an error handler with exception clauses: use the handler for the overall strategy and clauses for exception-specific behavior. Camel documents the Default Error Handler, Transaction Error Handler, and Dead Letter Channel among its strategies; features differ by strategy, so configure and verify the one appropriate to the route. See the Apache Camel Error Handler documentation.

How does Camel choose an onException clause?

A typed clause identifies the exception type whose failures it should govern. For example, a validation failure could be handled like this:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
onException(ValidationException.class)
    .handled(true)
    .transform(constant("INVALID REQUEST"));

from("direct:start")
    .bean("validator")
    .to("direct:continue");

This example is schematic. Confirm imports and DSL compatibility against the Camel release used by your project. In this clause, handled(true) means the failed route will end; the handler transforms the exchange body into the response. Camel’s Exception Clause documentation describes typed policies and Java DSL configuration.

Matching exception types and causes

Camel matches the thrown exception and nested causes against the configured types using instanceof-style matching, preferring the closest match. A route-level clause takes precedence over an equally close match configured at RouteBuilder scope. A clause with onWhen also requires its predicate to evaluate true.

This is policy selection, not Java’s source-order catch behavior. When the same exception is configured more than once in the same scope without onWhen, Camel’s documentation says the last configured clause is used; that rule should not be generalized to other configurations.

What is the difference between handled and continued?

Setting What happens to the failed route? What to put in the handler
handled(true) The original route ends at the failure. Build the final response or perform the failure work. If the handler does not construct a response, the documented caller outcome is an empty body.
continued(true) Camel suppresses the exception and resumes the original route from the failure point. Use it when later route processing should still run after the failure has been handled.

If the handler needs the original exception, read it from the exchange property Exchange.EXCEPTION_CAUGHT. In the documented handled-flow example, exchange.getException() is null because Camel has handled the exception. The Exception Clause guide explains this handling pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do retries and dead-letter routing fit in?

Redelivery can be configured on an error handler, an exception clause, or the applicable redelivery policy. An exception clause can cover multiple types in Java DSL:

onException(MyBusinessException.class, MyOtherBusinessException.class)
    .maximumRedeliveries(2);

The value shown is an example, not a recommended retry count. Set retries according to the operation’s semantics and whether repeating it is safe: a retry can repeat side effects, so consider idempotency before enabling redelivery. Delayed redelivery uses a scheduled thread pool by default; the executor can be configured. See the Exception Clause documentation for redelivery configuration.

Choose the failure destination through the broader error-handling design. The Default Error Handler propagates exceptions back to the caller; a Dead Letter Channel supports dead-letter routing. Transaction behavior and supported features depend on the selected error handler, rather than being interchangeable properties of every strategy. The Error Handler manual describes the available strategies.

Conditional policies and retry decisions

For advanced cases, onWhen can conditionally match an exception clause, while retryWhile can make retry decisions using a predicate. These solve different questions: whether the policy applies and whether another retry should be attempted. Consult the Exception Clause manual for the supported options in your Camel version.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should I use a local doTry/doCatch block?

Use a local block when the route needs localized try/catch/finally control flow rather than a policy shared across routes. Camel prefixes these Java DSL keywords with do and closes the block with end:

from("direct:start")
    .doTry()
        .bean("riskyOperation")
    .doCatch(IOException.class)
        .to("direct:ioFailure")
    .doFinally()
        .to("direct:cleanup")
    .end();

This is a schematic example, not a tested snippet; check syntax and imports for your Camel release. The important behavioral difference is that doTry/doCatch/doFinally is its own error handler. The normal Camel error handler and onException do not trigger for failures handled inside that block. The Try, Catch and Finally documentation covers this DSL.

A practical choice checklist

  • Use an error handler to select the broader route failure strategy, including propagation or dead-letter behavior.
  • Use onException for typed exception policies shared at builder scope or limited to a route.
  • Choose handled(true) to end the failed route and supply its outcome in the handler; choose continued(true) to resume the original route.
  • Use redelivery only when repeating the operation is appropriate, and configure the policy at the error-handler or exception-clause level that matches its intended scope.
  • Use doTry/doCatch for genuinely local flow, remembering that it bypasses the normal Camel error handler and onException for that block.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.