为什么Try / Catch块创建新的变量范围? [英] Why does a Try/Catch block create new variable scope?
问题描述
例如:
try
{
SomeObject someObject = new SomeObject();
someObject.dangerousMethod();
}
catch(例外e)
{
}
someObject.anotherMethod(); //无法访问someObject!
但你可以在 try / catch $ c $之前声明它c>阻止然后它工作正常:
SomeObject someObject;
try
{
someObject = new SomeObject();
someObject.dangerousMethod();
}
catch(例外e)
{
}
someObject.anotherMethod(); //工作正常
我只是想知道这个的设计原因。为什么在 try / catch
块中创建的对象不在方法的其余部分范围内?也许我不能深入了解 try / catch
如何工作除了只是注意抛出 Exceptions
。
为什么在try / catch块中创建的对象不在其他方法的范围内?
他们是。 try / catch
块中的变量声明 不在包含块的范围内,原因与所有其他变量声明对于它们发生的作用域是本地的:这就是规范定义它的方式。 :-)(更多内容,包括对你的评论的回复。)
这是中的创建的对象尝试/ catch
可在其外部访问:
SomeObject someObject = null;
try
{
someObject = new SomeObject();
someObject.dangerousMethod();
}
catch(例外e)
{
}
someObject.anotherMethod(); //这很好 - 除非SomeObject
//构造函数抛出异常,其中
// case someObject将为null
注意区别。 变量 p>
但是基于上面的方法名称,更有用的结构是:
SomeObject someObject = new SomeObject();
try
{
someObject.dangerousMethod();
}
catch(例外e)
{
}
someObject.anotherMethod();
重新评论:
我想我为什么为try / catch块创建了另一个范围感到困惑。
在Java中,所有块都创建范围。一个的主体,如果
,的主体
,的主体,而
等等 —它们都创建了一个新的嵌套变量范围:
if(foo){
SomeObject bar = new SomeObject() ;
}
bar.doSomething(); //< ==编译错误,`bar`未定义
(事实上,即使是没有任何控制结构的块会创建一个。)
如果你考虑它,它是有道理的:有些块是有条件的,比如定义<$的主体c $ c> if 或 while
。在上面的 if
中, bar
可能已经或未被声明(取决于 foo
),这没有任何意义,因为编译器当然没有 foo
的运行时值的概念。因此,为了保持一致性,Java的设计人员可以使用所有块创建一个新的嵌套范围。 ( JavaScript 的设计者走了另一条道路—根本没有块范围,但是,虽然它被添加了—而且这种方法也让人感到困惑。 )
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.
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.)
这篇关于为什么Try / Catch块创建新的变量范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!