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."
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()
.
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?
Edit: Here's the simplest code fragment that still triggers the problem:
try (Connection con = dataSource.getConnection()) {
/*...*/
}
Thoughts on whether or not this is related to the use of a JNDI DataSource?
Thanks in advance.
AutoClosable
implementation that do not throw an exception in whose case you would not need a catch anything, or you could add athrows
clause in your method signature in whose case you wouldn't need acatch
clause. – Scissile