Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try with multiple Resource in Java [duplicate]

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(nvl(rset.getString("MESSAGE")));                 ret.add(vo);             }         } 
like image 972
en Lopes Avatar asked Nov 08 '17 09:11

en Lopes


People also ask

Can we use multiple resources in try with resources?

We can declare multiple resources in a try block. Try initialization block can have any number of resources resulting in either null or non-null resources. In the below example, we can able to declare multiple resources in the try-with-resources statement.

What is try () in Java?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

What is the difference between try and try with resources?

The try -with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try -with-resources statement ensures that each resource is closed at the end of the statement.

Can we use try with resources without catch and finally?

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System. exit() it will execute always.


1 Answers

Try with resources can be used with multiple resources by declaring them all in the try block and this feature introduced in java 7 not in java 8 If you have multiple you can give like below

try (         java.util.zip.ZipFile zf =              new java.util.zip.ZipFile(zipFileName);         java.io.BufferedWriter writer =              java.nio.file.Files.newBufferedWriter(outputFilePath, charset)     ) {         // Enumerate each entry         for (java.util.Enumeration entries =                                 zf.entries(); entries.hasMoreElements();) {             // Get the entry name and write it to the output file             String newLine = System.getProperty("line.separator");             String zipEntryName =                  ((java.util.zip.ZipEntry)entries.nextElement()).getName() +                  newLine;             writer.write(zipEntryName, 0, zipEntryName.length());         }     } 

In this example, the try-with-resources statement contains two declarations that are separated by a semicolon: ZipFile and BufferedWriter. When the block of code that directly follows it terminates, either normally or because of an exception, the close methods of the BufferedWriter and ZipFile objects are automatically called in this order. Note that the close methods of resources are called in the opposite order of their creation..

Please see documentation for more info

like image 116
Vishnu T S Avatar answered Sep 26 '22 22:09

Vishnu T S