catch 块和资源关闭的真正工作顺序是什么?
What is the real sequence of work of the catch block and the resource closing?
在Oracle Java documentation on try-with-resources中写着:
A try-with-resources statement can have catch and finally blocks just
like an ordinary try statement. In a try-with-resources statement, any
catch or finally block is run after the resources declared have been
closed.
因此,根据文档,如果在尝试关闭资源时发生异常,并且我诚实地尝试以某种方式对那条悲伤的消息做出反应,如下所示:
try (OutputStream os = new SampleStream(true)) {
os.write(0); // both this and closing can throw IOWriteException
}
catch (IOWriteException e) {
//do something wise;
}
如果关闭有问题,catch 块将永远等待关闭。
我知道,事实并非如此,关闭时的 try-with-resources 异常可以捕获。但随后应重新制定所提及的规则。怎么样?
I know, that really it is not so, and the try-with-resources exception
on closing can be caught.
对了,还有关于资源的初始化。
我认为 the extended try-with-resources part of the JLS 可以帮助重新表述这个相当尴尬的解释。
而关于 finally
声明的部分是相当正确的。
我们可以这样说:
try-with-resources 语句中的 catch
语句允许捕获在该语句的任何部分抛出的兼容异常,即 1) 在资源初始化期间,2)在任何资源的资源关闭期间或 3)by 在 try-with-resources 主体中执行的语句.
关于 finally
语句,它将在资源关闭后执行 (或试图关闭)。
参考:
14.20.3.2. Extended try-with-resources
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
Catchesopt
Finallyopt
is given by the following translation to a basic try-with-resources
statement (§14.20.3.1) nested inside a try-catch
or try-finally
or
try-catch-finally
statement:
try {
try ResourceSpecification
Block
}
Catchesopt
Finallyopt
The effect of the translation is to put the ResourceSpecification
"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.
Furthermore, all resources will have been closed (or attempted to be
closed) by the time the finally
block is executed, in keeping with the
intent of the finally
keyword.
在Oracle Java documentation on try-with-resources中写着:
A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.
因此,根据文档,如果在尝试关闭资源时发生异常,并且我诚实地尝试以某种方式对那条悲伤的消息做出反应,如下所示:
try (OutputStream os = new SampleStream(true)) {
os.write(0); // both this and closing can throw IOWriteException
}
catch (IOWriteException e) {
//do something wise;
}
如果关闭有问题,catch 块将永远等待关闭。
我知道,事实并非如此,关闭时的 try-with-resources 异常可以捕获。但随后应重新制定所提及的规则。怎么样?
I know, that really it is not so, and the try-with-resources exception on closing can be caught.
对了,还有关于资源的初始化。
我认为 the extended try-with-resources part of the JLS 可以帮助重新表述这个相当尴尬的解释。
而关于 finally
声明的部分是相当正确的。
我们可以这样说:
try-with-resources 语句中的 catch
语句允许捕获在该语句的任何部分抛出的兼容异常,即 1) 在资源初始化期间,2)在任何资源的资源关闭期间或 3)by 在 try-with-resources 主体中执行的语句.
关于 finally
语句,它将在资源关闭后执行 (或试图关闭)。
参考:
14.20.3.2. Extended try-with-resources
A try-with-resources statement with at least one
catch
clause and/or afinally
clause is called an extended try-with-resources statement.The meaning of an extended try-with-resources statement:
try ResourceSpecification
Block
Catchesopt
Finallyopt
is given by the following translation to a basic try-with-resources statement (§14.20.3.1) nested inside a
try-catch
ortry-finally
ortry-catch-finally
statement:
try {
try ResourceSpecification
Block
}
Catchesopt
Finallyopt
The effect of the translation is to put the
ResourceSpecification
"inside" thetry
statement. This allows acatch
clause of an extended try-with-resources statement to catch an exception due to the automatic initialization or closing of any resource.Furthermore, all resources will have been closed (or attempted to be closed) by the time the
finally
block is executed, in keeping with the intent of thefinally
keyword.