aspose file tools
The moose likes Beginning Java and the fly likes Try/catch variable scope Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 56163
    
  13

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?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 56163
    
  13

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: 56163
    
  13

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Try/catch variable scope
 
Similar Threads
setAutoCommit(autocommit);
connecting to Webtrends reporting DB using JDBC-ODBC
connecting to oracle db problem
problem in connecting to oracle through jdbc
Sockets