You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by yli <yi...@gmail.com> on 2010/11/01 19:20:52 UTC

how to handle exceptions thrown by

Hi all,
      I am a newbie to Camel and recently I've been stuck in this problem(I
am using Camel with Spring):
      I defined an errorHandler in the CamelContext tag and I want it to be
applied to all of my routes. In one of my route, I use <camel: when> and
inside of that I bind a method in a POJO as the predicate. The problem is
that when the method throws an exception, it is not handle by the
errorHandler, instead, the exception is added to the exchange and the route
continues. 
      Example code:
      <camel:camelContext id="context" errorHandlerRef="myHandler">
           <camel:route id="myRoute">
               <camel:choice>
                   <camel:when>
                       <camel:method beanType="MyClass"
method="myMethodThatThrowsException"/>
                           do something... 
                   </camel:when>
                   <camel:otherwise>
                       do something else...
                   </camel:otherwise>
               </camel:choice>       
           </camel:route>

           other routes...
      </camel:camelContext>  

      <bean id="myHandler"
class="org.apache.camel.builder.DeadLetterChannelBuilder">
          <proeprty name="redeliveryPolicy" ref="myPolicy" />
          <property name="onRedelivery" ref="myAlert" /> 
          <property name="deadLetterUri" value="mock:dead" />      
      </bean>

      <bean id="myPolicy">
       ...
      </bean>
     
      <bean id="myAlert">
       ...
      </bean>  

       Is there a way that I can handle the exception thrown by
"myMethodThatThrowsException" using "myHandler"? 
       Please let me know if I missed anything. Any suggestion is
appreciated!  

Best
Yiming

-- 
View this message in context: http://camel.465427.n5.nabble.com/how-to-handle-exceptions-thrown-by-camel-choice-tp3245484p3245484.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: how to handle exceptions thrown by

Posted by Christian Müller <ch...@gmail.com>.
The easiest solution could be to catch your exception and return false (I
assume you throw an exception if the address string is not "well formed" for
you and in fact invalid).

However, where is your 'onException' definition? May be this link is helpful
for you: http://camel.apache.org/exception-clause.html (look for "Using
handeld with Spring DSL")

Christian

Re: how to handle exceptions thrown by

Posted by yli <yi...@gmail.com>.
I am sorry I forgot to post the code of the method that throws exceptions.

public MyClass{
       public static boolean myMethodThatThrowsException(Message in) throws
IllegalArgumentException{
           String address = in.getHeader("address", String.class);
           return isValidAddress(address);
       }

       private static isValidAddress(String address) throws
IllegalArgumentException{
           if (address.startWith("I'm valid"))
              return true;              
           if (address.startWith("I'm not valid"))
              return false;
           throw new IllegalArgumentException("Unknown");
       }
}

Thank you!
-- 
View this message in context: http://camel.465427.n5.nabble.com/how-to-handle-exceptions-thrown-by-camel-choice-tp3245484p3245506.html
Sent from the Camel - Users mailing list archive at Nabble.com.