Re: Checking for null parameter
Lew wrote:
public sendMessage(String message) throws ConnectionException =
{
assert conn != null;
try {
conn.sendMessage(message);
} catch (ConnectionException e) {
// Delegate the exception
throw new ConnectionException("Couldn't send message i=
n
pek wrote:
Oops, OK, correcting my self. Your code doesn't violate that. ;) I
went to see if you tried to assert the message parameter, which you
didn't, which means that if conn isn't null, and message is, the
conn.sendMessage(message) with throw a NPE (assuming it will use a
method of String).
WHy assume? Doesn't the Javadoc for the method tell you how it
handles null input?
So, not checking if message is null was intentionally or not? And if
I was making a point about 'assert' for the 'conn' variable. Adding a
lot of commentary about 'message' would have been off that point.
Also,l I simply assumed that sendMessage() did the right thing with
it, or you'd have checked for null yourself. I didn't want to second-
guess you.
you didn't check it because it was a String, if it where a type of
Message, what would you do? Would you check with assert? Would you
manually check it? Or none of the above and let a NPE be thrown?
That depends. Mostly if there were an exception I'd want to log it
and get the program back to sensible state. Just letting an exception
crash the program means that I can't clean up other resources, I can't
control the logging of the event, and I make an ugly experience for
the user. I much prefer to catch the exception, log it, and get back
on solid ground.
--
Lew