• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

goto inside a try catch block

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. If anybody has a minute, I've been wrestling with this for a while and can't seem to come up with a viable solution.

try {

for(int x=0,x<5,x++) {

...do junk...

//RESUME HERE
}

} catch(Exception e) {
System.out.println(e.toString()
//NEED A WAY TO RETURN TO RESUME HERE
}

I'm trying to handle any exception that occurs in "..do junk..." and return to the loop in order to grab the next iteration and avoid the rest of "...do junk...". I've tried ifs, switches, etc but they don't mesh with the try-catch. I'm probably missing something here but does anybody have any ideas...a java Goto would be nice but I know thats frowned upon cuz it makes code hard to decipher. Thank you very much for reading this.
[ December 11, 2007: Message edited by: Tom Griffith ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put the try/catch inside the for loop
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Move the try-catch block inside the loop:
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i forgot to note that the try encapsulates a jdbc connection and i want to loop through records using the same persistent connection object...so the try has to be outside the loop...

wait..maybe i just add another try-catch inside the loop to handle any exceptions in there...that's probably it...your posts got me thinking...
[ December 11, 2007: Message edited by: Tom Griffith ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Griffith:
a java Goto would be nice but I know thats frowned upon cuz it makes code hard to decipher.



It's not frowned upon -- in fact, you're encouraged to use it!

(I'm just kidding; the truth is that Java doesn't have goto.)
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Griffith:
wait..maybe i just add another try-catch inside the loop to handle any exceptions in there...that's probably it...your posts got me thinking...


Seems like the right way to handle this. The outer catch only handles SQLExecption, the inner catch handles the rest.

Of course this means you can't catch Exception in the inner catch, or it will include the SQLExecption.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic