This is a quote from Effective java.
"Where possible, the best way to deal with exceptions from lower layers is to avoid them, by ensuring that lower-level methods succeed. Sometimes you can do this by checking the validity of the higher-level method’s parameters before passing them on to lower layers."
Consider an object called "AccessControlContext actx" which is propogated from handler to lower levels. We can do a higher level check that "actx != null" but does it need to done in lower level again ?
Eg in psuedocode:
class RestServlet {
void verify Servlet(AccessControlContext actx) {
// check made at higher level according to effective java
if (actx == null) { throw exception; }
// do something
Verify.checkAccessControl(actx); // calling lower leve
// do something
}
}
class Verify {
static checkAccessControl(actx) {
// DO I NEED NULL POINTER CHECK HERE AGAIN ?
}
}
Although question is nested inside the comment, reiterating with elaboration. A redundant check at lower level ensures defensive coding but - it is redundant. It may be well spelled out in the javadocs that it does not accept null, but that does not solve the purpose of having a bug free code. Should we dedupe exception checks ?
NOTE: This was a random example. My question is not specific to this example. Its a broader question which seeks to understand how much to duplicate exceptions.