I never throw a NullPointerException . For me, this looks natural in the code when something goes wrong, and this requires the developer to see what happens. Then (s) he corrects the cause, and this does not happen again.
I am using IllegalStateException to signal that the object is configured incorrectly or that the calls are in the wrong order. However, we all know that, ideally, an object should guarantee that it cannot be in a bad state and that you cannot call it in the wrong order (create a constructor and the resulting object ...).
I use a lot of IllegalArgumentException when a method detects that its parameters are incorrect. It is the responsibility of any public method to stop processing (to avoid indirect errors that are more difficult to understand). In addition, several if at the beginning of the method serve the purpose of documentation (documentation that never diverges from code because it is code :-)).
public void myMethod(String message, Long id) { if (message == null) { throw new IllegalArgumentException("myMethod message can't be null");
I also use specific runtime exceptions to signal exceptional conditions at a higher level.
For example, if the module of my application could not be started, I may get a ModuleNotOperationalException exception (ideally a common code, for example, an interceptor, otherwise a specific code) when another module calls it. After this architectural decision, each module must deal with this exception in operations that call other modules ...
KLE 01 Oct '09 at 9:26 2009-10-01 09:26
source share