Here is the full pattern with all blocks working together:
FileReader reader = null;
try {
reader = new FileReader("data.txt");
int ch = reader.read();
System.out.println((char) ch);
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Read error");
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.out.println("Close failed");
}
}
}
Notice the nested try-catch inside finally just to close the reader. This is verbose and easy to get wrong. Java introduced a cleaner alternative called try-with-resources, which I'll cover later in this section.