25

I'm trying to retrieve the stacktrace from the onException handler in Apache Camel:

   <onException>
            <exception>java.lang.Exception</exception>
            <handled>
                <constant>true</constant>
            </handled>

            <setHeader headerName="exception">
                <simple>${exception}</simple>
            </setHeader>
   </onException>

However, the above only shows the exception rather than the entire stacktrace.

I understand that Camel stores the caught exception as a property on the Exchange with the key: Exchange.EXCEPTION_CAUGHT, but how can this be retrieved from the camel context routes file ?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123

1 Answers1

40

Use exception.stacktrace to get the stacktrace. See the variables listed in the table at this page: http://camel.apache.org/simple

<simple>${exception.stacktrace}</simple>

There is also a ${exception.message} to refer to the exception message itself.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Thanks, this worked a treat. Creating a bean was the other alternative I had in mind. –  Jan 24 '12 at 09:36
  • Thanks Claus. Just a question on this - is it also possible to use `exception.class` ? – vikingsteve Jan 03 '14 at 09:01
  • Ok, I answered this - yes, it is possible. But it's almost better to just use `exception` in the simple expression, since that gives you the class name and the message. – vikingsteve Jan 03 '14 at 09:07
  • Often camel puts exception into CamelExceptionCaught property, how to print stacktrace in this case? – divideByZero May 16 '16 at 14:50