Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a Try/Catch block create new variable scope?

For example:

try {     SomeObject someObject = new SomeObject();     someObject.dangerousMethod(); } catch(Exception e) { } someObject.anotherMethod(); //can't access someObject! 

But you can declare it before the try/catch block and then it works fine:

SomeObject someObject; try {     someObject = new SomeObject();     someObject.dangerousMethod(); } catch(Exception e) { } someObject.anotherMethod(); //works fine 

I'm just wondering the design reason for this. Why are Objects created within the try/catch block not in scope with the rest of the method? Maybe I'm not understanding deep down how a try/catch works besides just watching for Exceptions thrown.

like image 591
telkins Avatar asked Jul 25 '12 17:07

telkins


People also ask

Do try catch blocks have scope?

Try-Catch and Variable Scope -it is not available to the scope outside. We would have the same problem if we tried to return the price inside of the catch block. Here's a key point: Variables declared inside a try or catch block are local to the scope of the block.

Why variables defined in try Cannot be used in catch or finally in Java?

Variables in try block So, if you declare a variable in try block, (for that matter in any block) it will be local to that particular block, the life time of the variable expires after the execution of the block. Therefore, you cannot access any variable declared in a block, outside it.

What is the purpose of try catch blocks?

Try/catch blocks allow a program to handle an exception gracefully in the way the programmer wants them to. For example, try/catch blocks will let a program print an error message (rather than simply crash) if it can't find an input file. Try blocks are the first part of try/catch blocks.

Can we declare variable in catch block?

Variables declared within the try/catch block are not in scope in the containing block, for the same reason that all other variable declarations are local to the scope in which they occur: That's how the specification defines it. :-) (More below, including a reply to your comment.)


1 Answers

Why are Objects created within the try/catch block not in scope with the rest of the method?

They are. Variables declared within the try/catch block are not in scope in the containing block, for the same reason that all other variable declarations are local to the scope in which they occur: That's how the specification defines it. :-) (More below, including a reply to your comment.)

Here's an object created within a try/catch which is accessible outside of it:

SomeObject someObject = null; try {     someObject = new SomeObject();     someObject.dangerousMethod(); } catch(Exception e) { } someObject.anotherMethod(); // This is fine -- unless the SomeObject                             // constructor threw the exception, in which                             // case someObject will be null 

Note the difference. Where the variable is declared defines the scope in which it exists, not where the object was created.

But based on the method names and such above, the more useful structure for that would be:

SomeObject someObject = new SomeObject(); try {     someObject.dangerousMethod(); } catch(Exception e) { } someObject.anotherMethod(); 

Re your comment:

I guess I'm confused as to why another scope has even been created for a try/catch block.

In Java, all blocks create scope. The body of an if, the body of an else, of a while, etc. — they all create a new, nested variable scope:

if (foo) {     SomeObject bar = new SomeObject(); } bar.doSomething(); // <== Compilation error, `bar` is not defined 

(In fact, even a block without any control structure creates one.)

And if you think about it, it makes sense: Some blocks are conditional, like the one defining the body of an if or while. In the above if, bar may or may not have been declared (depending on the value of foo), which makes no sense because of course the compiler has no concept of the runtime value of foo. So probably for consistency, the designers of Java went with having all blocks create a new nested scope. (The designer of JavaScript went the other way — there is no block scope at all, yet, though it's being added — and that approach also confuses people.)

like image 139
T.J. Crowder Avatar answered Oct 04 '22 01:10

T.J. Crowder