java - Should exceptions be checked at lower level if higher level takes care of them -


this quote effective java.

"where possible, best way deal exceptions lower layers avoid them, ensuring lower-level methods succeed. can checking validity of higher-level method’s parameters before passing them on lower layers."

consider object called "accesscontrolcontext actx" propogated handler lower levels. can higher level check "actx != null" need done in lower level again ?

eg in psuedocode:

class restservlet {    void verify servlet(accesscontrolcontext actx) {         // check made @ higher level according effective java         if (actx == null) { throw exception; }           //         verify.checkaccesscontrol(actx); // calling lower leve         //     } }  class verify {    static checkaccesscontrol(actx) {         // need null pointer check here again ?    } }  

although question nested inside comment, reiterating elaboration. redundant check @ lower level ensures defensive coding - redundant. may spelled out in javadocs not accept null, not solve purpose of having bug free code. should dedupe exception checks ?

note: random example. question not specific example. broader question seeks understand how duplicate exceptions.

when comes validating parameters, in opinion can't often. way i'm used working, parameters validated in layers of code. if method can't handle null value (it lead nullpointer instance), should method make sure parameters passed aren't null. same goes methods bad parameter value cause , exception further down in layers.

the way see it, if bad parameter cause exception, doesn't matter whether or not exception thrown parameter validation @ high level or parameter validation @ low level, anyways better exception cause if there no validation @ (or worse, no exception , erroneous execution.)

by letting each method responsible validating input, if passthrough-method, 1 can make sure new method calling existing method prevented passing along bad parameter, since lead validation exception, no matter if method being called high level or low level method.

so, in example validate input in both methods, accompanying tests verifying validation behaves should. input validation easy one-liner, check out validate class of apache commons lang: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/validate.html

for instance:

import org.apache.commons.lang.validate; import org.apache.commons.lang.stringutils;  public class {    public void dosomething(final integer someint, final string somestring) {        validate.notnull(someint, "can't proceed when someint null!");        validate.istrue(!stringutils.isempty(somestring, "can't proceed when somestring null or empty!");        //do stuff    } } 

if validation fails, illegalargumentexception message defined second parameter thrown. tests this:

public class somethingtest {      private something;      @rule     public expectedexception expectedexception = expectedexception.none();      @before     public void setup() {                 = new something();     }      @test     public void dosomethingshouldfailifsomeintisnull() {         expectedexception.expect(illegalargumentexception.class);         expectedexception.expectmessage("someint null");          something.dosomething(null, "hello");    }     @test    public void dosomethingshouldfailifsomestringisempty() {         expectedexception.expect(illegalargumentexception.class);         expectedexception.expectmessage("somestring null or empty");          something.dosomething(123, "");    }     //more tests.. } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -