RE: [Avalon4:Fortress] Eating Crow (was Changes to RoleManager)
"Berin Loritsch" <[email protected]>
| Newsgroups | gmane.comp.jakarta.avalon.apps.devel,gmane.comp.jakarta.avalon.devel |
|---|---|
| Message-ID | <[email protected]> |
> From: [email protected] [mailto:[email protected]] > > Again Bloch advocates documenting all exceptions in javadoc including > unchecked exceptions using the @throws tag. > The @throws clause is put in, and the exceptions thrown are IllegalArgument exceptions that have *both* the name of the argument and the reason why it is not legal (i.e. it is null). That satisfies the documentation aspects. However, regarding NullPointerException vs. IllegalArgumentException, let me delve into a bit more reasoning. I have developed many applications where somewhere deep inside the internal logic of the app, a NullPointerException was thrown. Nine times out of ten, the NPE was because of some unchecked argument in some method. For example: class ExampleNPEFactory { private String m_message; public void setMessage( String message ) { m_message = message; } public String getMessage() { return m_message; } public String doSomethingWithMessage() { return getMessage().toUpper(); } } Client code would perform an operation on the doSomethingWithMessage() method, assuming the message was set by some other class. Either that, or they assumed that Java behaves like C++, and would provide a default empty string (I know I have made that mistake when I first started using Java). Bottom line is that until setMessage() is called, that class will be a breeding ground for NPEs. We would be able to protect ourselves from NPEs in this case by *both* providing a default (m_message="") *and* testing the argument of the setMessage() method. Only doing one of those would not fix the problem. When I see the NPEs, I look for code constructs that resemble the above ExampleNPEFactory--which is a waste of time if it is thrown from a method that checks its arguments. What's worse is that I have seen way too many examples of poor use of the NullPointerException. That is why I place it in the realm of JVM thrown exceptions and not "my code" thrown exceptions. When I see an IllegalArgumentException, I know the fault lies with *my* code, not the code I am calling. Typically, the programmers who take the time to throw an IllegalArgumentException also take the time to put a reasonable explanation as to why it is illegal (at least in my little universe, your mileage may vary). If a method/constructor cannot have any null arguments, it is _illegal_ to pass one in. Therefore, I have the proper justification for the IllegalArgumentException. BTW, There are a few other developers who have been out there for a while who share my viewpoint. It seems that they are on the Jakarta Commons list, and not here ;P That is why I favor IAE over NPE any day of the week--even if Sun does it. I mean, would you base your next project on Visual Basic just because "everyone else is doing it" or "it is the standard"?