为什么 Try/Catch 块会创建新的变量作用域? [英] Why does a Try/Catch block create new variable scope?

查看:32
本文介绍了为什么 Try/Catch 块会创建新的变量作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

试试{SomeObject someObject = new SomeObject();someObject.dangerousMethod();}捕获(例外 e){}someObject.anotherMethod();//无法访问someObject!

但是你可以在 try/catch 块之前声明它,然后它就可以正常工作了:

SomeObject someObject;尝试{someObject = new SomeObject();someObject.dangerousMethod();}捕获(例外 e){}someObject.anotherMethod();//工作正常

我只是想知道这样做的设计原因.为什么在 try/catch 块中创建的对象不在方法的其余部分的范围内?也许我并没有深入了解 try/catch 是如何工作的,而只是观察抛出的 Exceptions.

解决方案

为什么在 try/catch 块中创建的对象不在该方法的其余部分的范围内?

他们是.try/catch 块中声明的变量不在包含块的范围内,原因与所有其他变量声明都是局部的它们发生的范围:这就是规范定义它的方式.:-)(更多内容如下,包括对您评论的回复.)

这是一个在 try/catch创建的对象,它可以在它外部访问:

SomeObject someObject = null;尝试{someObject = new SomeObject();someObject.dangerousMethod();}捕获(例外 e){}someObject.anotherMethod();//这很好——除非 SomeObject//构造函数抛出异常,其中//case someObject 将为空

注意区别.变量声明的地方定义了它存在的范围,而不是对象创建的地方.>

但是根据上面的方法名称等,更有用的结构是:

SomeObject someObject = new SomeObject();尝试{someObject.dangerousMethod();}捕获(例外 e){}someObject.anotherMethod();

<小时>

重新评论:

<块引用>

我想我很困惑为什么甚至为 try/catch 块创建了另一个作用域.

在 Java 中,所有块都创建作用域.if 的主体、else 的主体、while 的主体等. —它们都创建了一个新的嵌套变量作用域:

if (foo) {SomeObject bar = new SomeObject();}bar.doSomething();//<== 编译错误,`bar` 未定义

(事实上,即使是一个没有任何控制结构的块也会创建一个.)

如果你仔细想想,这是有道理的:有些块是有条件的,比如定义 ifwhile 主体的块.在上面的if中,bar可能已经声明,也可能没有声明(取决于foo的值),当然这是没有意义的编译器没有 foo 的运行时值的概念.所以可能是为了一致性,Java 的设计者让 all 块创建了一个新的嵌套作用域.(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆