In my code I have something like this
// 1st method
if (a == null) {
return false;
}
if (a.getB() == null {
return false;
}
if (a.getB().getC() == null {
reutrn false;
}
return a.getB.getC().isFoo();
Which is clear but very wordy. Meanwhile, something like this is less wordy but maybe not as clear because it's using a try-catch in a nontraditional manner.
// 2nd method
try {
return a.getB().getC().isFoo();
} catch(NullPointerException e) {
return false;
}
Is there any reason to specifically not use the 2nd method, is it a bad practice? As an aside, is there any reason one should be used or is preferred over the other?