when to throw exception if illegal argument passed to an API method?
<p>See the two examples below. Assume both classes contains public methods belonging to a widely used API library. </p>

<p><code>AClass.java</code> is easier to write (maybe easier to read due to less noise), but when error is detected, the first method in stack trace is an internal method (<code>nullCheck(...)</code>) and the exception message is not referring to that method. Could this cause unnecessary confusion to the user? I mean in the sense of the user thinking: "it's an internal method which has thrown an exception, must be a bug in the library, not a fault in my program. What a <em>*</em>* useless library..."</p>

<p><code>BClass.java</code> is more tiresome to write (with all its if statements), but when error is detected the first line of the stack trace pinpoints the API method (called by user) where error was first detected. Would this more likely make user think: "I'm calling that method from my code, must be something wrong with the parameter I'm passing in".</p>

<p>Which would be the preferred way of throwing exception when checking parameters for validity in a public API? Or are the two examples considered equal?</p>

<pre><code>public class AClass {
public void publicApiMethod1(String a){
    nullCheck(a, "a");
    // do something...
}
public void publicApiMethod2(String a, String b, String c){
    nullCheck(a, "a");
    nullCheck(b, "b");
    nullCheck(c, "c");
    // do something...
}

private void nullCheck(Object a, String argName) {
    if(a == null){
        throw new NullPointerException("Null argument: " + argName);
    }
}
}


public class BClass {
public void publicApiMethod1(String a){
    if(a == null){
        throw new NullPointerException("Null argument: 'a'");
    }
    // do something...
}

public void publicApiMethod2(String a, String b, String c){
    if(a == null){
        throw new NullPointerException("Null argument: 'a'");
    }
    if(b == null){
        throw new NullPointerException("Null argument: 'b'");
    }
    if(c == null){
        throw new NullPointerException("Null argument: 'c'");
    }
    // do something...
}
}
</code></pre>
