5 Replies Latest reply on Jun 22, 2009 2:21 PM by Brett Levasseur

    Throwing exceptions on camel routes

    Brett Levasseur Newbie



      I have a camel route (using version where I am receiving some xml files and then sending them to different locations depending on their content. I am doing this using the choice statement and xpath to determine where each of the xml files belong. In the otherwise portion of my choice statement I collect all of the files that did not belong anywhere else.


          <to uri="file:src/main/data/unrecognizedRequests?noop=true"></to>


      At this point I also want to throw an exception so that my onException portion of the route can pick up the event and record it with other errors. My problem is that I do not know how to write the xml configuration so that it will throw an error. So how would I write my otherwise statement above to also throw an exception? Thanks.


      Edited by: brett on Jun 19, 2009 1:58 PM

        • 1. Re: Throwing exceptions on camel routes
          Torsten Mielke Apprentice

          See http://camel.apache.org/message-router.html:



          Choice without otherwise


          If you use a choice without adding an otherwise, any unmatched exchanges will be dropped by default. If you prefer to have an exception for an unmatched exchange, you can add a throwFault to the otherwise.


          ....otherwise().throwFault("No matching when clause found on choice block");



          ... and try


          Also see


          • 2. Re: Throwing exceptions on camel routes
            Brett Levasseur Newbie

            I have tried using , however now I am getting a Type Conversion error.

            org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: class org.apache.camel.impl.DefaultMessage to the required type: java.io.InputStream with value Message: org.apache.camel.CamelException: java.lang.Exception
                    at org.apache.camel.impl.converter.DefaultTypeConverter.doConvertTo(DefaultTypeConverter.java:147)
                    at org.apache.camel.impl.converter.DefaultTypeConverter.convertTo(DefaultTypeConverter.java:90)
                    at org.apache.camel.impl.converter.DefaultTypeConverter.convertTo(DefaultTypeConverter.java:86)
                    at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:85)
                    at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:52)
                    at org.apache.camel.component.http.DefaultHttpBinding.doWriteResponse(DefaultHttpBinding.java:122)
                    at org.apache.camel.component.http.DefaultHttpBinding.doWriteFaultResponse(DefaultHttpBinding.java:95)
                    at org.apache.camel.component.http.DefaultHttpBinding.writeResponse(DefaultHttpBinding.java:62)
                    at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:76)
                    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
                    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:502)
                    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:363)
                    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
                    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
                    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
                    at org.mortbay.jetty.Server.handle(Server.java:324)
                    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:534)
                    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:879)
                    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:741)
                    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:207)
                    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:403)
                    at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
                    at org.mortbay.jetty.security.SslSocketConnector$SslConnection.run(SslSocketConnector.java:636)
                    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:522)


            I tried to look up how to create my own type converter to get around this. According to http://camel.apache.org/type-converter.html I can make a type converter by making a Converter class and method like the following:


            public class DefaultMessageConverter {
                 public static InputStream toInputStream(DefaultMessage dm)  {
                      return new ByteArrayInputStream(dm.getExchange().getIn().getBody().toString().getBytes());


            The directions also specify to add a file called TypeConverter to the directory META-INF/services/org/apache/camel/ and include the name of the package that my DefaultMessageConverter class is in. I did all of this but I sill get the exact same error. Are the directions listed at http://camel.apache.org/type-converter.html only for Camel 2.0 and not Does anyone see any reason why my converter would not be picked up?



            • 3. Re: Throwing exceptions on camel routes
              Claus Ibsen Master



              That is a bug.


              I have created a ticket



              You can use a Processor to throw the exception. Then its a real exception and should work better.




              .process(new MyExceptionThrower)


              And then just throw your exception from this processor in the process method.

              • 4. Re: Throwing exceptions on camel routes
                Claus Ibsen Master

                In Spring DSL then use



                • 5. Re: Throwing exceptions on camel routes
                  Brett Levasseur Newbie

                  Thanks, that worked.




                  Edited by: brett on Jun 22, 2009 6:21 PM