18

Have to throw an exception in my camel route defined in XML. Found throwException statement available from Camel 2.3 which looks like:

 <throwException ref="forced"></throwException>

However, I don't know how to define forced exception class to be thrown. Since same exception could be thrown couple of times with different exception messages - would be good to know if throwException has some other form of definition so exception class and exception message are defined in-place.

Archer
  • 5,073
  • 8
  • 50
  • 96

2 Answers2

19

The ref is just a reference to a so you can do

<bean id="forced" class="java.lang.IllegalArgumentException">
   <constructor-arg index="0" value="This is forced"/>
</bean>

<camelContext ...>
  ...
</camelContext>
Manglu
  • 10,744
  • 12
  • 44
  • 57
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • 2
    You can also look in the src/test/java directory of the source code of camel-spring, to find examples how to use in XML – Claus Ibsen Apr 30 '11 at 07:02
15

Since version 2.16.0 there is more elegant way to do it, with optional exception message:

<route>
     <throwException exceptionType="java.lang.IllegalArgumentException" message="illegal argument found"/>
</route>
Enigo
  • 3,685
  • 5
  • 29
  • 54