aspose file tools*
The moose likes Java in General and the fly likes variables accessible to catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "variables accessible to catch block" Watch "variables accessible to catch block" New topic
Author

variables accessible to catch block

Ektaa Gargg
Greenhorn

Joined: Mar 08, 2008
Posts: 9
I want to understand if variables declared outside /inside try block are /can be accessible to
catch block as well.

Please find a code template below:

Basically can i use Stmt like this inside catch block to capture exceptions(logging errors)

void insertException()
{

Connection conn = null;
PreparedStatement stmt = null;

try
{


}
catch(SQLException sqle)
{
String expQuery = "insert into tablename values(' ', ' ',' ');
stmt = conn.prepareStatement(expQuery); -- this should be again inside a try catch block
int catch1 = stmt.executeUpdate();
}

what can be the alternative to this.

Thanks

Ekta Garg


Ekta Garg<br />(Never say Die,Life is too short to be disheartened.)
Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
If variable declaration is in side the try block than scope of this variable is up to that try block. if you want to access the same variable in the catch block also then you have to declare this inside the catch block.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Abhay Choubey wrote:If variable declaration is in side the try block than scope of this variable is up to that try block. if you want to access the same variable in the catch block also then you have to declare this inside the catch block.


That is not entirely correct.

Every time you use a right bracket { you start a new code block and declares a new scope. Every time you use a left bracket }, you close the scope and the code block.

Everything that is declared inside a scope/block is visible inside of that block. Regardless if there are sub-blocks.

This means that if we have this pseudo-code:


and we are standing at *the watchpoint*, we can access the C declared inside the catch, E that is declared in the catch-call, B that is declared in the method and A that is declared in the clazz. However, the C that are declared inside of the catch-block is NOT the same as declared inside the try-block. That C is gone when we reach the // end try.

When we reach *after catch*, we can only see B and A. Everything else is gone. So, if you want to reach a variable in your catch, you need to declare it outside of try. The assignment of the declared variable can be inside of the try however...
Abhay Choubey
Ranch Hand

Joined: Jun 02, 2011
Posts: 34
whatever you are telling is true same thing i want to say that if you want to access the variable inside the catch block you have to declare out side the try block other wise it will not work in catch block. You have to declare in catch block also.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
The rules for scope for try-catch blocks are exactly the same as for anything else. If you declare something in the try and declare something with the same identifier in the catch, that will be a different variable.
You cannot declare two local variables within the same scope.
You can declare a local variable in the same scope as a field, in which case the local variable hides the field, but that is probably not usually a good idea.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variables accessible to catch block