autocloseable

Try with multiple Resource in Java [duplicate]

两盒软妹~` 提交于 2019-12-03 19:28:58
问题 This question already has answers here : Close multiple resources with AutoCloseable (try-with-resources) (4 answers) Closed 6 months ago . I am new in Java8 , and I want to know if, for the AutoCloseable resource, I have to add a try for each resource , or it will work with the code above try (Connection conn = getConnection();) { Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery(sql); while (rset.next()) { TelefonicaDataVO vo = new TelefonicaDataVO(); vo

Try-With Resource when AutoCloseable is null

偶尔善良 提交于 2019-12-03 10:22:07
How does the try-with feature work for AutoCloseable variables that have been declared null ? I assumed this would lead to a null pointer exception when it attempts to invoke close on the variable, but it runs no problem: try (BufferedReader br = null){ System.out.println("Test"); } catch (IOException e){ e.printStackTrace(); } The Java Language Specification specifies that it is closed only if non-null, in section 14.20.3. try-with-resources : A resource is closed only if it initialized to a non-null value. This can actually be useful, when a resource might present sometimes, and absent

Why is try-with-resources catch block selectively optional?

ⅰ亾dé卋堺 提交于 2019-12-03 08:19:19
问题 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

Why is try-with-resources catch block selectively optional?

余生长醉 提交于 2019-12-02 22:01:37
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

Eclipse inconsistencies: Resource leak: '<unassigned Closeable value>' is never closed

最后都变了- 提交于 2019-12-01 21:13:55
问题 If I have the following code: public OutputStream test(boolean condition) throws FileNotFoundException { return condition ? null : new FileOutputStream("test.txt"); } Eclipse puts yellow squiggles under new FileOutputStream("test.txt") and shows me the following warning: Resource leak: '<unassigned Closeable value>' is never closed The strange thing is, if I remove the ternary operation: public OutputStream test() throws FileNotFoundException { return new FileOutputStream("test.txt"); } the

Is it meaningful for AutoCloseable's close method to throw an exception? How should this be handled?

邮差的信 提交于 2019-12-01 20:53:02
In C#, it is considered bad practice to throw exceptions in the Dispose method of an IDisposable . By contrast, in java the close method of AutoCloseable allows any Exception whatsoever to be thrown and forces the caller to handle it somehow. But what is the caller reasonably expected to do if this happens? This suggests that the attempt to close the resource failed somehow. So does the user have to try to close the resource again before continuing, perhaps with some sort of exponential backoff? It looks like every operation involving the resources, including the implicit close() invocation,

My own solution for Kotlin's try-with-resources absence

我怕爱的太早我们不能终老 提交于 2019-12-01 04:26:51
Kotlin provides the use function for Closeable objects, but it seems they forgot to consider AutoCloseable (e.g. DB prepared statements) for the try-with-resources full Java equivalent. I've implemented the next "home-made" solution: inline fun <T:AutoCloseable,R> trywr(closeable: T, block: (T) -> R): R { try { return block(closeable); } finally { closeable.close() } } Then you can use it the next way: fun countEvents(sc: EventSearchCriteria?): Long { return trywr(connection.prepareStatement("SELECT COUNT(*) FROM event")) { var rs = it.executeQuery() rs.next() rs.getLong(1) } } I'm new to

My own solution for Kotlin's try-with-resources absence

怎甘沉沦 提交于 2019-12-01 02:15:16
问题 Kotlin provides the use function for Closeable objects, but it seems they forgot to consider AutoCloseable (e.g. DB prepared statements) for the try-with-resources full Java equivalent. I've implemented the next "home-made" solution: inline fun <T:AutoCloseable,R> trywr(closeable: T, block: (T) -> R): R { try { return block(closeable); } finally { closeable.close() } } Then you can use it the next way: fun countEvents(sc: EventSearchCriteria?): Long { return trywr(connection.prepareStatement(

Try with multiple Resource in Java [duplicate]

怎甘沉沦 提交于 2019-11-30 09:10:01
This question already has an answer here: Close multiple resources with AutoCloseable (try-with-resources) 4 answers I am new in Java8 , and I want to know if, for the AutoCloseable resource, I have to add a try for each resource , or it will work with the code above try (Connection conn = getConnection();) { Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery(sql); while (rset.next()) { TelefonicaDataVO vo = new TelefonicaDataVO(); vo.setTelefonicaDataId(rset.getString("Telefonica_PSD_ID")); vo.setReceptionDate(nvl(rset.getTimestamp("CREATION_DATE"))); vo.setMessage

What does idempotent method mean and what are the side effects in case of calling close method of java.lang.AutoCloseable?

谁说我不能喝 提交于 2019-11-28 06:22:01
Java docs of close() method of java.lang.AutoCloseable says Note that unlike the close() method of Closeable , this close() method is not required to be idempotent . In other words, calling this close method more than once may have some visible side effect, unlike Closeable#close() which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent. What do they mean by idempotent method and what are the side effects of calling this close() method twice? And since interface Closeable extends