Checking for null parameter
- From: pek <kimwlias@xxxxxxxxx>
- Date: Mon, 9 Jun 2008 02:37:00 -0700 (PDT)
So is checking for null pointer a good practice? I mean, 90% of the
times you have any object as a parameter, you really want that object
to be something other than null. Otherwise you have to blot your code
into an unlimited number of lines of try, catch etc (and sometimes you
will fail doing any correct error handling).
Just to make my question more clear, here is an example:
class Test {
private A a;
public Test(A a) throws Exception {
if ( a == null ) throw new Exception("Null!");
this.a = a
}
}
class Hell {
private Devil d;
private Fire f;
public Hell(Devil d, Fire f) throws Exception {
if ( ( d == null ) || ( f == null ) ) throw new
Exception("Null!");
this.d = d; this.f = f;
}
public void burnAngel(Angel a) {
if ( a == null ) throw new Exception("Null!");
a.burn();
}
}
class Main {
public static void main(String[] args) {
try {
Test t = new Test(new A());
} catch (Exception e) {}
try {
Hell h = new Hell(new Devil(), new Fire());
} catch (Exception e){}
}
}
So, is this all necessary? I mean, if A, Devil, Fire and Angel is null
and I know that they should not be, then that means that I have an
error in my code. There is no reason to explicity tell someone that
the object should not be null. If I do, I have to try catch almost 80%
of my code.
What do you think? If checking for null overkill or not?
.
- Follow-Ups:
- Re: Checking for null parameter
- From: Roedy Green
- Re: Checking for null parameter
- From: Daniel Pitts
- Re: Checking for null parameter
- From: Robert
- Re: Checking for null parameter
- From: Ulrich Eckhardt
- Re: Checking for null parameter
- From: ttrifonov
- Re: Checking for null parameter
- From: Lew
- Re: Checking for null parameter
- Prev by Date: Re: Iterating across a TreeMap....
- Next by Date: Re: Looking for Technical Architects to work with a SEI CMMI 5Level company! Urgent!!
- Previous by thread: Looking for Technical Architects to work with a SEI CMMI 5Level company! Urgent!!
- Next by thread: Re: Checking for null parameter
- Index(es):
Relevant Pages
|