# Exception Handling

## Overview
This example demonstrates how to handle exceptions that occur while routing messages with Camel.

This example show you how to add a default error handler to your Camel context for all uncaught exceptions. Additionally, it will show you how to add exception handling routines for dealing with specific exception types.

## What You Will Learn
In studying this example you will learn:

* how to define a Camel route using the Blueprint XML syntax
* how to build and deploy a Fuse Application Bundle (FAB) in Fuse ESB
* how to define a default error handler to your Camel context
* how to define exception-specific error handling routines

## Prerequisites
Before building and running this example you need:

* Maven 3.0.3 or higher
* JDK 1.6
* Fuse ESB Enterprise 7

## Files in the Example
* `pom.xml` - the Maven POM file for building the example
* `src/main/java/org/fusesource/examples/errors/OrderService.java` - a Java class used to validate the orders being processed by the route. It generates exceptions when an order is placed on a Sunday and also at random intervals.
* `src/main/java/org/fusesource/examples/errors/OrderValidationException.java` - a Java class defining the exception thrown when an order is invalid
* `src/main/resources/OSGI-INF/blueprint/errors.xml` - the OSGI Blueprint file that defines the route
* `test/data/*.xml` - data files that can be used to test the example
* `src/test/java/org/fusesource/examples/errors/OrderServiceTest.java` - a JUnit test class
* `src/test/resources/log4j.properties` - configuration for formatting the test output

## Building the Example
To build the example:

1. Change your working directory to the `examples/errors` directory.
2. Run `mvn clean install` to build the example.

## Running the Example
To run the example:

1. Start Fuse ESB Enterprise 7 by running bin/fuseesb (on Linux) or bin\fuseesb.bat (on Windows).
2. In the Fuse ESB console, enter the following command: `osgi:install -s fab:mvn:org.fusesource.examples/errors/7.0.1.fuse-084`
3. As soon as the Camel route has been started, you will see a directory `work/errors/input` in your Fuse ESB installation.
4. Copy the file you find in this example's `src/test/data` directory to the newly created `work/errors/input` directory.
5. Wait a few moment and you will find the files in directories under `work/errors`:
** `order4.xml` will always end up in the `work/errors/validation` directory
** other files will end up in `work/errors/output` or `work/errors/deadletter` depending on the runtime exceptions that occur
6. Use `log:display` to check out the business logging - the exact output may look differently because the 'unexpected runtime exception...' happen randomly
        Processing order4.xml
        Order validation failure: order date 2012-03-04 should not be a Sunday
        Validation failed for order4.xml - moving the file to work/errors/validation
        Processing order5.xml
        An unexcepted runtime exception occurred while processing order5.xml
        Done processing order5.xml
        ...

## More information
For more information see:

* http://www.enterpriseintegrationpatterns.com/DeadLetterChannel.html for the Dead Letter Channel EIP
* http://fusesource.com/documentation/fuse-esb-enterprise-documentation for more information about using Fuse ESB Enterprise
