[rabbitmq-discuss] How to close connection and channel when an exception occur

Tim Watson watson.timothy at gmail.com
Fri Sep 27 20:50:42 BST 2013


You need to add the exception to a throws clause or surround the statement with a try-catch.

On 27 Sep 2013, at 15:24, Phil <cwstorm at gmail.com> wrote:

> Okay.  Here's my code:
> 
> ConnectionFactory factory = new ConnectionFactory();
> factory.setUri("amqp://guest:guest@host:5672");
> Connection conn = factory.newConnection();
> Channel channel = conn.createChannel();
> 
> Got compiling error:
> 
> unreported exception java.net.URISyntaxException; must be caught or declared to be thrown
> 		factory.setUri("");
>                               ^
> unreported exception java.io.IOException; must be caught or declared to be thrown
> 		Connection conn = factory.newConnection();
>                                                        ^
> unreported exception java.io.IOException; must be caught or declared to be thrown
> 		Channel channel = conn.createChannel();
> 
> On Friday, September 27, 2013 10:13:24 AM UTC-4, Phil wrote:
> Thank you everyone and you guys are absolutely correct.  I thought I had to declare the connection and channel inside the try block because exception need to be thrown or caught. 
> 
> Sent from mobile device with autocorrect. 
> 
> Phil 
> 
> > On Sep 27, 2013, at 9:40 AM, Tim Watson <tim at rabbitmq.com> wrote: 
> > 
> > Phil, 
> > 
> >> On 27 Sep 2013, at 14:31, Phil wrote: 
> >> I'm a newbie on RabbitMQ.  I'm writing a java service to consume a message from the queue.  Everything is working as designed but I cannot seem to code "channel.close() or conn.close()" in the catch  or finally block.  Java is complaining about that the "cannot find symbol". 
> >> 
> >> symbol  : variable channel 
> >> location: class RouteToMercury.javaServices 
> >>            channel.close(); 
> > 
> > It's impossible to help with a compiler error if you don't post that code that you're trying to compile. 
> > 
> >> Can I close the connection and channel if an exception occur? 
> > 
> > Yes, absolutely. The above looks like a scoping issue. I suspect you've done something like... 
> > 
> > try { 
> >  final Channel channel = connection.createChannel(); 
> >  doSomeWork(channel); 
> > } finally { 
> >  channel.close(); 
> > } 
> > 
> > Am I right? If so, you need to move the channel variable to the outer scope. 
> > 
> > Cheers, 
> > Tim 
> > _______________________________________________ 
> > rabbitmq-discuss mailing list 
> > rabbitmq-discuss at lists.rabbitmq.com 
> > https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss 
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130927/a31ee13c/attachment.htm>


More information about the rabbitmq-discuss mailing list