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Â Â Â Â ...