gpt4 book ai didi

java - 尝试使用资源返回。这是 JVM 的正确行为吗?

转载 作者:行者123 更新时间:2023-11-30 10:38:44 24 4
gpt4 key购买 nike

在下面的代码示例中,我希望将 1 作为方法 testM() 的返回值。但是由于 TestAutoCloseable.close() 方法中的异常,我得到了意外的行为。

我的问题是:“这是 JVM 的正常行为吗?”

public static void main(String[] args) {
ProgrammerTwo programmerTwo = new ProgrammerTwo();
System.out.println(programmerTwo.testM());
}

int testM() {
try (TestAutoCloseable closable = new TestAutoCloseable()) {
System.out.println("Do first return");
return 1;
} catch (IOException e) {
System.out.println("handled");
}
System.out.println("Do something, that shouldn't do if first return have happened");
return 2;
}

static class TestAutoCloseable implements AutoCloseable {

@Override
public void close() throws IOException {
throw new IOException();
}
}

因为如果这是正常行为,我们不应该在 try with resources 语句中使用 return 或 break 语句。它应该是反模式。

最佳答案

try-with-resources 语句如何工作的细节在 this section 中。来自 JLS。在你的例子中,它是一个extended try-with-resources 因为它有一个 catch 子句定义在下面的引用中(注意突出显示的最后声明)。

A try-with-resources statement with at least one catch clause and/or a finally clause is called an extended try-with-resources statement.

The meaning of an extended try-with-resources statement:

try ResourceSpecification
Block
[Catches]
[Finally]

is given by the following translation to a basic try-with-resources statement nested inside a try-catch or try-finally or try-catch-finally statement:

try {
try ResourceSpecification <--- exception thrown in this basic try-with-resources
Block
}
[Catches]
[Finally]

The effect of the translation is to put the resource specification "inside" the try statement. This allows a catch clause of an extended try-with-resources statement to catch an exception due to the automatic initialization or closing of any resource.

这意味着关闭资源发生在外部try block 的主体内,导致异常被抛出并在catch 中处理> block ,控制恢复到扩展的 try-with-resources 语句之后的语句。

实际上,整个方法 testM 等同于:

int testM() {
try {
final TestAutoCloseable closable = new TestAutoCloseable();
Throwable #primaryExc = null;
try {
System.out.println("Do first return");
return 1;
} catch (Throwable #t) {
#primaryExc = #t;
throw #t;
} finally {
if (closable != null) {
if (#primaryExc != null) {
try {
closable.close();
} catch (Throwable #suppressedExc) {
#primaryExc.addSuppressed(#suppressedExc);
}
} else {
closable.close();
}
}
}
} catch (IOException e) {
System.out.println("handled");
}
System.out.println("Do something, that shouldn't do if first return have happened");
return 2;
}

关于java - 尝试使用资源返回。这是 JVM 的正确行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39595742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com