try-with-resources

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

Is flush() call necessary when using try-with-resources

半腔热情 提交于 2019-11-30 08:12:10
Will try-with-resources call flush() implicitly? If it does, in the following code snippet, bw.flush() can be safely removed? static void printToFile1(String text, File file) { try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { bw.write(text); bw.flush(); } catch (IOException ex) { // handle ex } } ps. I don't see any description about it in official document: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html Closeable and AutoCloseable are general-purpose interfaces that

Why write Try-With-Resources without Catch or Finally?

扶醉桌前 提交于 2019-11-30 04:45:26
Why write Try without a Catch or Finally as in the following example? protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet tryse</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet tryse at " + request.getContextPath() +

Why does the ExecutorService interface not implement AutoCloseable?

↘锁芯ラ 提交于 2019-11-29 05:31:36
问题 Failing to call shutdown() on a thread executor will result in a never terminating application. Best practice to shut down the ExecutorService is this: ExecutorService service = null; try { service = Executors.newSingleThreadExecutor(); // add tasks to thread executor … } finally { if (service != null) service.shutdown(); } Since Java knows the try-with-resources concept, wouldn't it be nice if we could do this? try (service = Executors.newSingleThreadExecutor()) { // add tasks to thread

Why write Try-With-Resources without Catch or Finally?

Deadly 提交于 2019-11-29 02:16:21
问题 Why write Try without a Catch or Finally as in the following example? protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet tryse</title>"); out

IntelliJ IDE gives error when using Try-Catch with Resources

谁说我不能喝 提交于 2019-11-28 17:45:28
I am attempting to use JDK 7's "try-catch with resources" statement; IntelliJ highlights my resource line, saying Try-with-resources are not supported at this language level. When I try to compile, I get: java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources) I checked that try-with-resources is enabled for my current project, and that my project is using JDK 7 (Library: C:\Program Files\Java\jdk1.7.0_11). Any ideas? I can't figure out what option to change (if that's even the issue). Click on the File menu, open Project Structure, then

Transaction rollback on SQLException using new try-with-resources block

▼魔方 西西 提交于 2019-11-28 16:31:08
I have a problem with try-with-resources and I am asking just to be sure. Can I use it, if I need to react on exception, and I still need the resource in catch block? Example given is this: try (java.sql.Connection con = createConnection()) { con.setAutoCommit(false); Statement stm = con.createStatement(); stm.execute(someQuery); // causes SQLException } catch(SQLException ex) { con.rollback(); // do other stuff } I fear that I am still doomed to use the old try-catch-finally in this case, even according to oracle documentation - "catch and finally blocks in a try-with-resources statement, any

8 branches for try with resources - jacoco coverage possible?

那年仲夏 提交于 2019-11-28 16:18:28
I've got some code that uses try with resources and in jacoco it's coming up as only half covered. All the source code lines are green, but I get a little yellow symbol telling me that only 4 of 8 branches are covered. I'm having trouble figuring out what all the branches are, and how to write code that covers them. Three possible places throw PipelineException . These are createStageList() , processItem() and the implied close() Not throwing any exceptions, throwing an exception from createStageList() throwing an exception from processItem() throwing an exception from close() throwing an

Try-with-resources and return statements in java

混江龙づ霸主 提交于 2019-11-28 10:40:15
I'm wondering if putting a return statement inside a try-with-resources block prevents the resource to be automatically closed. try(Connection conn = ...) { return conn.createStatement().execute("..."); } If I write something like this will the Connection be closed? In the Oracle documentation it is stated that: The try-with-resources statement ensures that each resource is closed at the end of the statement. What happens if the end of the statement is never reached because of a return statement? Based on Oracle's tutorial , "[the resource] will be closed regardless of whether the try

Try-with-resources in Kotlin

你。 提交于 2019-11-28 04:47:52
When I tried to write an equivalent of a Java try -with-resources code in Kotlin, it didn't work for me. I tried different variations of the following: try (writer = OutputStreamWriter(r.getOutputStream())) { // ... } But neither works. Does anyone know what should be used instead? Apparently Kotlin grammar doesn't have definition for such a construct, but maybe I'm missing something. It defines grammar for try block as follows: try : "try" block catchBlock* finallyBlock?; There is use -function in kotlin stdlib ( src ). How to use it: OutputStreamWriter(r.getOutputStream()).use { // by `it`