wood burning stoves*
The moose likes Beginning Java and the fly likes loop try and catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loop try and catch" Watch "loop try and catch" New topic
Author

loop try and catch

Mart Thorpe
Greenhorn

Joined: Jul 30, 2007
Posts: 8
I have a loop with a try and catch statement inside. If the try fails and the catch is executed do I need to break out of the loop or is this done by the catch automatically?

Thanks for reading.

cheers
Martin
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
That depends on the logic that you are trying to implement.In case you want to break in case of exception then put it in catch as catch would not automatically do it.

Or if you want your loop to continue then just catch the exception , thats all .The loop will continue.


Rahul Bhattacharjee
LinkedIn - Blog
Mart Thorpe
Greenhorn

Joined: Jul 30, 2007
Posts: 8
Thanks Rahul.

Yeah I want to break out of the loop, I put in a break just wondering if I needed it. Thanks for the speedy reply.

cheers
Martin
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's one way to get out of the loop:

Now if there is an exception in the loop, the catch is outside the loop and you cannot get back in.

But if you structure it this way ...

you can have exception every time through the loop and still keep going.

Would one of those match what you're doing?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mart Thorpe
Greenhorn

Joined: Jul 30, 2007
Posts: 8
Hello Stan, thanks for the reply.

Yes your second example is what I am doing already but if an exception is thrown then I want to stop the loop and not keep running, in my case this would give an infinite loop, so I want to break out.

My question was that would the catch break out of the loop automatically or do I need to code it, Rahul confirmed my suspicions that I do indeed need to use break to stop the loop when an exception is thrown.

Thanks for reading and thank you for your reply.

cheers
Martin
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
... I do indeed need to use break to stop the loop when an exception is thrown.

What Stan was showing with the first construct is that you do not need a *break* statement to exit the loop on an exception.

Explicit use of the *break* keyword is almost never necessary outside of a switch construct. In fact, many style guidelines explicitly prohibit its use as it can obfuscate code flow.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mart Thorpe
Greenhorn

Joined: Jul 30, 2007
Posts: 8
Hello Garret.

Excellent, that was what I needed to know, originally, if I explicitly need a break in my catch statement or not.

I should have posted some code with my original post.

I have this already



So do I need those breaks there? Or will either the interrupt or monitor state break the loop for me?

Thank you for the reply, and thanks for reading.

cheers
Martin
[ July 31, 2007: Message edited by: Mart Thorpe ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Garrett Rowe:
Explicit use of the *break* keyword is almost never necessary outside of a switch construct. In fact, many style guidelines explicitly prohibit its use as it can obfuscate code flow.


They often prohibit "continue", for similar reasons.

Avoiding "break" (outside "switch") and "continue" (everywhere) is generally good style. I personally do not agree with total prohibition; occasionally a loop is more readable when coded with "break" or "continue" than without.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Originally posted by Mart Thorpe:
So do I need those breaks there?


That's one way, but some alternative ways of coding it areor


Joanne
Mart Thorpe
Greenhorn

Joined: Jul 30, 2007
Posts: 8
ok here is my full snippet where re is a request returned from a server.

I guess I could put the try round the whole while() loop but whilst I was using break for something else, in the if(), I just kind of used it for the exceptions too.

So would the code be better putting the try catch around the while() loop?

If so what makes it better, performance?

cheers
Martin

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loop try and catch
 
Similar Threads
Beep Sound continuously for 1 min??
for loop prob - please HELP!!
Solution:How to skip Exceptions in While Loop and continue.
try-catch logic design issue
Try-Catch Handling