Though the author is correct in advising to do exception handling like this[1] instead of this[2], they are not correct in the reasoning behind it. In method [2], if a writer cannot be instanciated, the code will still try to write with it, which will probably cause a NullPointerException. Rewriting to method [1] hinders this.
[1]:
Writer writer = null;
try {
writer = new FileWriter("/tmp/a");
writer.write("Line1");
writer.close();
} catch(FileNotFoundException e) {
// handle error
} catch (IOException ex) {
// handle error
}
[2]:
Writer writer = null;
try {
writer = new FileWriter("/tmp/a");
} catch(FileNotFoundException e) {
// handle error
} catch (IOException ex) {
// handle error
}
try {
writer.write("Line1");
} catch (IOException e) {
// handle error
}
try {
writer.close();
} catch (IOException e) {
// handle error
}