简体   繁体   English

尝试资源的资源范围

[英]Try-with-resources scope of resource

In the try -with-resources construct of Java 7 , I can declare a resource in the try statement, and it will be closed automatically when it goes out of scope. Java 7try -with-resources结构中,我可以在try语句中声明一个资源,当它超出范围时它将自动关闭。

However, I don't find any indication of the scope of the resource made available. 但是,我没有发现任何可用资源范围的迹象。 Specifically, is it available in the catch / finally blocks of the try block where it is declared? 具体来说,是否可以在try块的catch / finally块中声明它?

I tried the following in Eclipse Kepler , but it's giving a mixed impression: 我在Eclipse Kepler中尝试了以下内容,但它给人的印象是:

Resource variable is made available by Content Assist (Code Completion) : 资源变量由内容辅助(代码完成)提供

内容辅助建议资源

Quick Fix suggests changing to resource variable, but this recursively produces the same problem it's trying to fix: 快速修复建议更改为资源变量,但这会递归地产生它尝试修复的相同问题:

快速修复中的冗余建议

I would like to know what the correct scope limitation is, before raising a bug in the Eclipse Bug Tracker . 在提出Eclipse Bug Tracker中的错误之前,我想知道正确的范围限制是什么。

This syntax is called Extended try-with-resources 此语法称为Extended try-with-resources

As per JLS : 根据JLS

try ResourceSpecification
    Block
Catchesopt
Finallyopt

Will be translated to: 将被翻译为:

try {
    try ResourceSpecification
        Block
}
Catchesopt
Finallyopt

So, in your example, your resource will be limited to inner try block, so not available for outer try/catch/finally . 因此,在您的示例中,您的资源将仅限于内部try块,因此不适用于外部try/catch/finally

EDIT: 编辑:

my question does not have nested try blocks 我的问题没有嵌套的try块

By explicitly adding catch/finally block in your code, you are introducing nested try blocks. 通过在代码中显式添加catch / finally块,您将引入嵌套的try块。

Update from 2017 after Java 9 release Java 9发布后,2017年更新

Now with Java 9 we have more syntactic sugar and we can have a resource declared outside the try-catch block but still handled properly. 现在使用Java 9我们有更多的语法糖,我们可以在try-catch块之外声明一个资源,但仍然可以正确处理。 That's why with Java 9 the Try-With-Resources has been improved introducing a new syntax: 这就是为什么在Java 9中,Try-With-Resources得到了改进,引入了一种新的语法:

InputStream stream = new MyInputStream(...)
try (stream) {
   // do something with stream being sure that is going to be closed at the end
} catch(IOException e) {
   // you can surely use your resource here
}

Note that this syntax will result in a compile time error for Java version 8 or minor 请注意,此语法将导致Java版本8或次要版本的编译时错误

This is more "natural" way of writing even though in most use cases we don't need the resource outside the scope of the try block. 这是更“自然”的写作方式,即使在大多数用例中我们不需要try块范围之外的资源。 The only restriction is that the reader variable should be effectively final or just final. 唯一的限制是读者变量应该是有效的最终或最终。

Anyway with this syntax you can surely have your resource used also in the catch and finally block 无论如何,使用这种语法,你肯定也可以在catchfinally块中使用你的资源

The correct scope limitation is within the declaration part (...) and the actual try block. 正确的范围限制在声明部分(...)和实际的try块中。

The JLS states JLS表示

The scope of a variable declared in the ResourceSpecification of a try-with-resources statement (§14.20.3) is from the declaration rightward over the remainder of the ResourceSpecification and the entire try block associated with the try-with-resources statement. 在try-with-resources语句(第14.20.3节)的ResourceSpecification中声明的变量的范围来自于ResourceSpecification的其余部分以及与try-with-resources语句关联的整个try块的向右声明。

So from the point it is declared in the ResourceSpecification (...) of the try onwards until the final closing } bracket of the try Block . 因此,从try anwards的ResourceSpecification (...)中声明它直到try Block的最后结束}括号。

TryWithResourcesStatement:
    try ResourceSpecification Block Catchesopt Finallyopt

ResourceSpecification:
    ( Resources ;opt )

Resources:
    Resource
    Resource ; Resources

Resource:
    VariableModifiersopt Type VariableDeclaratorId = Expression

In addition to @Nambari's answer: 除了@ Nambari的回答:

A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. try-with-resources语句可以像普通的try语句一样有catch和finally块。 In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed. 在try-with-resources语句中,在声明的资源关闭后运行任何catch或finally块。

That pretty much explains the behaviour, your resource goes out of scope in your explicit catch/finally block. 这几乎解释了行为,您的资源超出了显式catch / finally块的范围。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM