Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try/catch variable scope

 
Alessandro Camel
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!
 
bhanu chowdary
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64688
86
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?
 
Suhas Mandrawadkar
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
Pie
Posts: 64688
86
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
Pie
Posts: 64688
86
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic