commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sean C. Sullivan" <s...@seansullivan.com>
Subject [httpclient] parameter validation, NullPointerException vs IllegalArgumentException
Date Sun, 10 Feb 2002 21:46:51 GMT
From: "dIon Gillard" <dion@multitask.com.au>
>
> Sean C. Sullivan wrote:
>
> >NameValuePair.java
> >
> >--------------------------
> >
> >    /**
> >     * Constructor.
> >     */
> >    public NameValuePair(String name, String value) {
> >
> >        this.name = name;
> >        this.value = value;
> >
> >    }
> >
> >--------------------------
> >
> >With this constructor, it is allowable for a caller to do this:
> >
> >    NameValuePair nvp = new NameValuePair(null, null);
> >
> >I would prefer if the NameValuePair constructor did not allow null
parameters.
> >
> >I prefer this constructor:
> >
> >    /**
> >     * Constructor.
> >     */
> >    public NameValuePair(String name, String value) {
> >
> >        if (null == name) {
> >            throw new NullPointerException("name parameter is null");
> >        }
> >        if (null == value) {
> >            throw new NullPointerException("value parameter is null");
> >        }
> >
> >        this.name = name;
> >        this.value = value;
> >
> >    }
> >
> >
> Again,
>
> my call would be if these are illegal arguments, it should throw
> IllegalArgumentException, not NullPointerException.


In the core Java packages (java.*), Sun throws a NullPointerException
when a null parameter is unexpectedly detected:

Example:  java/io/File.java

////////////////////
    public File(String pathname) {
                 if (pathname == null) {
                         throw new NullPointerException();
                 }
           /// .... etc .....
    }

//////////

Also, Josh Bloch recommends throwing a NullPointerException
when a parameter is unexpectedly null ( "Item 23: Check Parameters for
Validity", [1], pages 120-121 ).

-Sean


[1] Effective Java Programming Language Guide by Josh Bloch
             http://www.amazon.com/exec/obidos/ASIN/0201310058/



--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message