• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop try and catch

 
Mart Thorpe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mart Thorpe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mart Thorpe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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.
 
Mart Thorpe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mart Thorpe:
So do I need those breaks there?


That's one way, but some alternative ways of coding it areor
 
Mart Thorpe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic