aspose file tools*
The moose likes Beginning Java and the fly likes Try/catch variable scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try/catch variable scope" Watch "Try/catch variable scope" New topic
Author

Try/catch variable scope

Alessandro Camel
Ranch Hand

Joined: Jul 03, 2009
Posts: 35
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

Joined: Mar 09, 2010
Posts: 256
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

Joined: Jan 10, 2002
Posts: 61419
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

Declare connection outside try block.

Connection connection;
try{
}


Regards, Suhas S. Mandrawadkar.
Certifications: SCJP 6, SCWCD 5, Oracle WebLogic Server Administrator, OCE Java EE 6 EJB Developer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

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

Joined: Jul 03, 2009
Posts: 35
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

Joined: Mar 09, 2010
Posts: 256
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

Joined: Jan 10, 2002
Posts: 61419
    
  67

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try/catch variable scope