Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Problem with "Try" blocks and variables - JDBC 2a

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my code I am trying to create a connection object (the same thing I'm about to describe happens with statement and result set objects as well) within a Try, Catch block. My problem is that I can't seem to close the objects within the same "tier" of Try, Catch blocks. It seems the compiler doesn't recognize my variable when I do this. The only way I've been able to get it to work is to have another (nested?) set of Try, Catch blocks and do the closing inside this inner block.

Here's an example of what I'm talking about:



I've been able to work around this except for one case where I'm trying to write a method that will return a connection object. Presumably, for this to work, I have to have my "return connection object" statement as the last statement in my method, but I can't figure out how create an object inside the method that I can pass because of the way I'm having to create the objects inside try, catch blocks (I can't make them available outside the blocks). The only way I can get it to work at all is by using a global variable and I finally understand that's a bad thing
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it seems to me this is an access issue. If you declare your local variables in the TRY{} block, those variables will only be visible within that code block.

You can try putting, the declaration of your variables outside the TRY{} block so they can be visible/accesible from the whole method.

Thanks.

a-s
 
Matt Fry
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Andres. However, I'm not sure how to declare a Connection object without it being inside a try, catch block to catch errors that might come up with it.

So, if I declare a variable inside a try{} block, is it not accessible to the catch{} and finally{} portions of the same "tier" (I know that's probably not the correct terminology but I think y'all can understand what I'm saying? This seems to be that case and if it is, how would one go about declaring a variable [that can throw an exception] to be used throughout a method?
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's important to understand the difference between declaring a variable, and initializing it. If you are not sure, it is well worth reading and searching a bit more until you can see the difference.

The first one has no side effects, and can safely be done outside a "try", the second should (in the case of creating a JDBC Connection, at least) be done inside a "try".

Does that help?
 
Matt Fry
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Frank.

I'm pretty sure I understand the difference between the two although one probably couldn't tell it from my posts (sorry about that). Here's some code I tried that's along the lines of what you are saying:



When I tried to compile this, I got an error saying that the variable con may not have been initialized properly. I think I understand why I got this error but I'm not sure how to do it without this error.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if you initialize connection to null to start with. Then close it in a finally block.
 
Matt Fry
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That works! Thanks again, Marilyn!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic