Java examples for Language Basics:try catch finally
Manage the closing of effectively final variables in Java 9
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { try { writeFile(new BufferedWriter( new FileWriter("test")), "This is easy in Java 9"); } catch (IOException ioe) { System.out.println(ioe); } } public static void writeFile(BufferedWriter writer, String text) { try (writer) { writer.write(text); } catch (IOException ioe) { System.out.println(ioe); } } }
Prior to Java 9, the writeFile would have looked as follows:
public static void writeFile(BufferedWriter writer, String text) { try (BufferedWriter w = writer) { w.write(text); } catch (IOException ioe) { System.out.println(ioe); } }