This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Try/catch variable scope  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have a problem with a variable scope!

The variable is declared into a try/catch block, but later I need it in order to call a method, but I can't see it!
I am talking about the variable connection :



What is the correct way to go round this ?
Thanks!
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare the variable connection before the try block. This should your problem. With your code the scope of the variable is confined to the try block
 
Author and ninkuma
Marshal
Posts: 66804
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you need it outside of the try block? If the code fails, is it meaningful to try and use a variable that may not be valid?

Perhaps the structure of your code is not quite right?
 
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare connection outside try block.

Connection connection;
try{
}

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66804
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suhas Mandrawadkar wrote:Declare connection outside try block.


Which will cause the error to go away, but will it be correct? Is the structure of the code improved by this approach?
 
Alessandro Camel
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Why would you need it outside of the try block? If the code fails, is it meaningful to try and use a variable that may not be valid?

Perhaps the structure of your code is not quite right?



I am quite sure that the structure is bad
I am not quite confident with the OOP.

My exercize is:
Read line by line a csv file
Insert every line inside a mysql table

My idea is using a method to group the insert task code to avoid all the sql lines inside the while-read-file block.

 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:If the code fails, is it meaningful to try and use a variable that may not be valid?


But Bear, in case if the code fails we may not be doing what we do when there is a successful connection created
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66804
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bhanu chowdary wrote:But Bear, in case if the code fails we may not be doing what we do when there is a successful connection created



I'm not sure what you are trying to say. But my point is that if the purpose of the try block is to make sure that a good connection is established, why would you reference it outside of the try block where you do not know if it has been successful or not?
 
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!