为什么try-with-resources catch块选择性地可选? [英] Why is try-with-resources catch block selectively optional?
问题描述
我读到try-with-resources中的 catch
块是可选的。
我尝试在try-with-resources块中创建一个 Connection
对象,没有后续的 catch
阻止,仅从eclipse中获取编译器错误:
自动 close()$ c $抛出未处理的异常类型
SQLException
c>调用。
I read that the catch
block in try-with-resources is optional.
I've tried creating a Connection
object in a try-with-resources block, with no subsequent catch
block, only to get compiler error from eclipse:
"Unhandled exception type SQLException
thrown by automatic close()
invocation."
由于可以在try-with-resources中使用的每个资源都实现 AutoCloseable
,并且因此,在调用 close()
方法时可能会抛出异常,我不明白 catch
子句是怎么回事可选,因为它不允许我跳过从 close()
中捕获异常。
Since every resource that can be used in try-with-resources implements AutoCloseable
, and so potentially throws an exception upon invocation of the close()
method, I don't understand how the catch
clause is optional, given that it's not allowing me to skip catching the exception from close()
.
是否有一些特殊要求, AutoCloseable
的具体实现不直接声明其<$ c中抛出的任何异常$ c> close()方法? (例如,覆盖 AutoCloseable
的 close()抛出异常
,其中 close()
哪个不会抛出任何异常)?
Is there some special requirement that the specific implementation of AutoCloseable
not directly declare any exception thrown in its close()
method? (e.g. override AutoCloseable
's close() throws Exception
with a close()
which does not throw any Exception)?
..或者这可能只是一个日食问题?
..or is this possibly just an eclipse issue?
编辑:这是仍然是最简单的代码片段触发问题:
Here's the simplest code fragment that still triggers the problem:
try (Connection con = dataSource.getConnection()) {
/*...*/
}
关于这是否与使用有关的想法一个JNDI数据源?
Thoughts on whether or not this is related to the use of a JNDI DataSource?
提前致谢。
推荐答案
如果 close()
无法投掷,则可选检查异常。但是,如果 close()
可以,则需要以正常方式处理已检查的异常,使用 catch
阻止,或者通过抛出 try-with-resources
块的方法。
It is optional if close()
is not able to throw a checked exception. However, if close()
can, then a checked exception would need to handled in a normal fashion, either with a catch
block, or by throwing from the method that try-with-resources
block is in.
更多细节是在 JLS 14.2.3
14.20.3.2。扩展的try-with-resources
14.20.3.2. Extended try-with-resources
带有至少一个catch子句和/或finally子句的try-with-resources语句称为扩展的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.
扩展的try-with-resources语句的含义:
The meaning of an extended try-with-resources statement:
try ResourceSpecification
Block
[Catches]
[Finally]
由以下翻译给出嵌套在try-catch或try-finally或try-catch-finally语句中的基本try-with-resources语句:
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
Block
}
[Catches]
[Finally]
翻译的效果是将资源规范放在try语句的内部。这允许扩展的try-with-resources语句的catch子句由于自动初始化或关闭任何资源而捕获异常。
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.
此外,所有资源都将具有在执行finally块时关闭(或尝试关闭),与finally关键字的意图保持一致。
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.
关于这是否与使用JNDI数据源有关的想法?
是的,确实如此。
在您提供的示例 try-with-resourses 块中,有必要捕获异常并处理,或者从块所在的方法,因为 SQLException
是一个已检查的异常。
In the example try-with-resourses block you've provided, it is necessary to catch the exception and handle, or throw from the method the block is in, because SQLException
is a checked exception.
这篇关于为什么try-with-resources catch块选择性地可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!