Re: NullPointerException, IllegalArgumentException, or AssertionError
for null constructor argument
Zig wrote:
Next, we know that a constructor is a special kind of method,
There are many important differences between a constructor and a method, of
sufficient magnitude that it is more useful to think of constructors not being
methods at all.
They do, however, share the ability to receive parameters and throw exceptions
with methods. Zig's main point about argument checking is therefore completely
applicable, of course.
With respect to the rest of those points, let me warn that Sun's libraries are
not free of unfortunate choices. They may not always represent current best
practices.
The choice of NPE or IllegalArgumentException is the classic Java angel's ball
[1] on the head of a pin. You should use NPE when you decide that users of
your library should be primarily aware of the nullness of the argument. You
should use IllegalArgumentException when you decide that users of your class
should be primarily aware of its illegality. You use the latter with the
former as a cause when you decide that users should be primarily aware of the
illegality and that it's illegal on account of nullness. The driver of your
choice is your what you will for client classes to experience.
Personally, I choose the third, combination option for clients of my code
pretty much always, because pretty much always it's the nature of the argument
as an argument that's important for the client class to track, and whether
it's legal is the first consideration, why it's illegal the second.
[1] ball in the sense of a formal dance.
--
Lew