You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by james555 <lu...@gmx.net> on 2014/06/17 15:05:39 UTC

nested catch-blocks in doCatch and doFinally

Hello,

I need to use nested catch-blocks in my project like the following:

from("direct:start")						
.doTry()
	.setBody(simple("doTry"))
	.to("stream:out")					
.doCatch(Exception.class)
	.setBody(simple("doCatch"))
	.to("stream:out")			
	.doTry()
		.setBody(simple("doTry in doCatch"))
		.to("stream:out")
	.doCatch(Exception.class)
		.setBody(simple("doCatch in doCatch"))
		.to("stream:out")
	.end()												
.doFinally()
	.setBody(simple("doFinally"))
	.to("stream:out")			
	.doTry()
		.setBody(simple("doTry in doFinally"))
		.to("stream:out")
	.doCatch(Exception.class)
		.setBody(simple("doCatch in doFinally"))
		.to("stream:out")					
	.end()					
	.stop()							
.endDoTry();


Compiler says: the method doFinally() is undefined for the type
ProcessorDefinition<capture#1-of ?> so Eclipse suggests  ("add cast to
method receiver") following:



((TryDefinition) from("direct:start")						
.doTry()
	.setBody(simple("doTry"))
	.to("stream:out")					
.doCatch(Exception.class)
	.setBody(simple("doCatch"))
	.to("stream:out")			
	.doTry()
		.setBody(simple("doTry in doCatch"))
		.to("stream:out")
	.doCatch(Exception.class)
		.setBody(simple("doCatch in doCatch"))
		.to("stream:out")
	.end()
	)												
.doFinally()
	.setBody(simple("doFinally"))
	.to("stream:out")			
	.doTry()
		.setBody(simple("doTry in doFinally"))
		.to("stream:out")
	.doCatch(Exception.class)
		.setBody(simple("doCatch in doFinally"))
		.to("stream:out")					
	.end()					
	.stop()							
.endDoTry();


but then at runtime I get:

java.lang.ClassCastException: org.apache.camel.model.CatchDefinition cannot
be cast to org.apache.camel.model.TryDefinition


	Whats wrong here, I can't see a syntax error ? Is it a bug ?
	
		thanks for any advice
		
			james



--
View this message in context: http://camel.465427.n5.nabble.com/nested-catch-blocks-in-doCatch-and-doFinally-tp5752429.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: nested catch-blocks in doCatch and doFinally

Posted by james555 <lu...@gmx.net>.
thanks for replying, Willem. I've tried that workaround before and it works.



--
View this message in context: http://camel.465427.n5.nabble.com/nested-catch-blocks-in-doCatch-and-doFinally-tp5752429p5752498.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: nested catch-blocks in doCatch and doFinally

Posted by Willem Jiang <wi...@gmail.com>.
There are two doTry blocks in your camel route which could confuse the compiler, as Camel Java DSL is not good at blocks.
You can work around the issue by using the direct endpoint to avoid using doTry inside of doCatch just like this

            from("direct:start")    
                .doTry() 
                        .setBody(simple("doTry")) 
                        .to("stream:out")       
                .doCatch(Exception.class) 
                        .setBody(simple("doCatch")) 
                        .to("stream:out")       
                        .to("direct:anotherDoTry") 
                .doFinally() 
                        .setBody(simple("doFinally")) 
                        .to("stream:out")       
                        .doTry() 
                                .setBody(simple("doTry in doFinally")) 
                                .to("stream:out") 
                        .doCatch(Exception.class) 
                                .setBody(simple("doCatch in doFinally")) 
                                .to("stream:out")       
                        .end()  
                        .stop() 
                .endDoTry(); 
                
                from("direct:anotherDoTry")
                .doTry() 
                    .setBody(simple("doTry in doCatch")) 
                    .to("stream:out") 
                .doCatch(Exception.class) 
                    .setBody(simple("doCatch in doCatch")) 
                    .to("stream:out") 
                .end(); 


--  
Willem Jiang

Red Hat, Inc.
Web: http://www.redhat.com
Blog: http://willemjiang.blogspot.com (English)
http://jnn.iteye.com (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem



On June 17, 2014 at 9:06:08 PM, james555 (luke333@gmx.net) wrote:
> Hello,
>  
> I need to use nested catch-blocks in my project like the following:
>  
> from("direct:start")
> .doTry()
> .setBody(simple("doTry"))
> .to("stream:out")
> .doCatch(Exception.class)
> .setBody(simple("doCatch"))
> .to("stream:out")
> .doTry()
> .setBody(simple("doTry in doCatch"))
> .to("stream:out")
> .doCatch(Exception.class)
> .setBody(simple("doCatch in doCatch"))
> .to("stream:out")
> .end()
> .doFinally()
> .setBody(simple("doFinally"))
> .to("stream:out")
> .doTry()
> .setBody(simple("doTry in doFinally"))
> .to("stream:out")
> .doCatch(Exception.class)
> .setBody(simple("doCatch in doFinally"))
> .to("stream:out")
> .end()
> .stop()
> .endDoTry();
>  
>  
> Compiler says: the method doFinally() is undefined for the type
> ProcessorDefinition so Eclipse suggests ("add cast to
> method receiver") following:
>  
>  
>  
> ((TryDefinition) from("direct:start")
> .doTry()
> .setBody(simple("doTry"))
> .to("stream:out")
> .doCatch(Exception.class)
> .setBody(simple("doCatch"))
> .to("stream:out")
> .doTry()
> .setBody(simple("doTry in doCatch"))
> .to("stream:out")
> .doCatch(Exception.class)
> .setBody(simple("doCatch in doCatch"))
> .to("stream:out")
> .end()
> )
> .doFinally()
> .setBody(simple("doFinally"))
> .to("stream:out")
> .doTry()
> .setBody(simple("doTry in doFinally"))
> .to("stream:out")
> .doCatch(Exception.class)
> .setBody(simple("doCatch in doFinally"))
> .to("stream:out")
> .end()
> .stop()
> .endDoTry();
>  
>  
> but then at runtime I get:
>  
> java.lang.ClassCastException: org.apache.camel.model.CatchDefinition cannot  
> be cast to org.apache.camel.model.TryDefinition
>  
>  
> Whats wrong here, I can't see a syntax error ? Is it a bug ?
>  
> thanks for any advice
>  
> james
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/nested-catch-blocks-in-doCatch-and-doFinally-tp5752429.html  
> Sent from the Camel - Users mailing list archive at Nabble.com.
>  


Re: nested catch-blocks in doCatch and doFinally

Posted by james555 <lu...@gmx.net>.
additional Information: camel version is 2.13.0



--
View this message in context: http://camel.465427.n5.nabble.com/nested-catch-blocks-in-doCatch-and-doFinally-tp5752429p5752481.html
Sent from the Camel - Users mailing list archive at Nabble.com.