Link to home
Start Free TrialLog in
Avatar of isiauna2009
isiauna2009

asked on

validate date not null and throw exception

Hello Experts,
I am totally lost. I am trying to validate a perameter which is (GregorianCalender, date ) the validation must not be null.
I was using an if statement and used (date != null). I have to throw an illegalArugmrnt and I used  {
         IllegalArgumentException exception = new IllegalArgumentException ();
            throw exception;
        }
My test keeps failing, it is my code. How can I Validate that the given parameter date is not null.

What am I doing wrong?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

if (date!=null) {
   throw new IllegalArgumentException("Date cannot be null");
}
Avatar of jcott28
jcott28

Probably need a bit more of your code.  To see if a parameter is not null is what you've got.  You can see if (object == null) and handle it that way or check to make sure its != null.
ASKER CERTIFIED SOLUTION
Avatar of Justin Mathews
Justin Mathews

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>
Shouldn't you be saying:
..

Instead of:
>>

Absolutely ;)
There's actually no reason you need to catch it in a real program. Passing null would be an error and runtime would deal with it.
> Shouldn't you be saying:

good pickup :)
I better get some coffee
Hm, interesting discussion :-)
May be you have catched the exception in the same method, instead of declare the method in this way:
...validate(Date dt) throws IllegalArgumentException {
You might also like to throw for dates that are out of range