My problem is this: I have a rather long getter, i.e.
objectA.getObjectB().getObjectC().getObjectD().getObjectE().getName();
Due to the poor database / entity architecture (some things were introduced later than others), it happens that getObjectB() , getObjectC() or getObjectD() can return NULL .
Usually we use null checks all the time, but in this case I would have to use
ObjectB b = objectA.getObjectB(); if (b != null) { ObjectC c = b.getObjectC(); if (c != null) { ObjectD d = c.getObjectD(); if (d != null) return d.getObjectE().getName(); } } return "";
Instead, it would be much easier to use a try-catch block.
try { return objectA.getObjectB().getObjectC().getObjectD().getObjectE().getName(); } catch (NullPointerException e) { return ""; }
In this case, I don’t care which object returns NULL, it either displays the name or not. Are there any complications or poor design for using try-catch instead of checks?
Thanks for your input.
java
LordAnomander
source share