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.
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.
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.
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.
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.)
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With