The finally Block
The finally
block can be used to execute some common code after any of the try-catch
blocks used to handle a series of different exceptions in the code. Going back to our example where we tried to open a non-existing file, a modified version of it including a finally
statement would look like the following:
Example12.java
11 try { 12 // provoke an exception 13 lines = Files.readAllLines(Paths.get("readme.txt")); 14 } catch (NoSuchFileException fe) { 15 System.out.println("Exception: File Not Found"); 16 } catch (IOException ioe) { 17 ...