Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variables accessible to catch block

 
Ektaa Gargg
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abhay Choubey
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic