• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Try / Catch Confusion

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a query I'm trying to wrap a Try / Catch block around, but having troubles.

This works:
String mer = "select xxxx from xxx where xxxx";
ResultSet mers = stmt.executeQuery(mer);
mers.next();

But this doesn't work:
try {
String mer = "select xxxx from xxx where xxxx";
ResultSet mers = stmt.executeQuery(mer);
mers.next();
} catch (SQLException e) { out.print("arrrrgh"); }


Why isn't this try / catch block working ?
I get errors saying that "mers" doesn't exist... which probably means that the code in the try block didn't even get to excecute.
Why not ?
 
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't seem to have much to do with JSP. Moving to the Java in General(intermediate) forum.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That would give the type of compiler error you're describing.
[ June 04, 2004: Message edited by: Blake Minghelli ]
 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is, this is valid and works perfectly:

String mer = "select xxxx from xxx where xxxx";
ResultSet mers = stmt.executeQuery(mer);
mers.next();


Why when I wrap a Try / Catch block around it does it not work ?
I though the purpose of the Try / Catch pragma was to execute the Try block, and *only if* there are any problems, execute the Catch code.

 
Bear Bibeault
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake's point is that if you are referencing mers outside of the try catch block, you will get an error since the scope of mers is confined to the block just as if it were in an "if", "for" or other block.

A common idiom:

 
Bear Bibeault
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Just saying "it doesn't work" isn't very helpful. Please be sure to include the detailed and exact error message you are getting when asking questions.
 
Dylan Stamatopolis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh... that makes too much sense
Thanks you very much !
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's weird, I just noticed that the first half of my original post above is missing, i.e. everything I said before the code tags???
You guys must be thinking "Gee, Blake's post was sure helpful (dripping sarcasm here)".
I wonder if that happened after I edited the post...
 
Bear Bibeault
Marshal
Posts: 67275
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a bug in UBB when editing posts with code tags. Not your fault.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!