- Featured Products
- Brainwave Tuner – 2009 Handango Championship Award
- Self-Hypnosis for Meditation- Quiet Your Mind With Self Hypnosis!
- Self-Hypnosis for Weight Control – Control Your Weight With Self Hypnosis!
- Relaxus – Tunes for Relaxation and a Good Night Sleep
- Self-Hypnosis for Sound Sleep – Enjoy Sound Sleep with Self-Hypnosis
- Brain Sooth – Sooth your mind and relieve headaches
- Buddhist Meditation – Enrich Your Hearts with Buddhist Melodies
- Brain Booster – Refresh Your Mind and Get a Mental Boost
- iZen – The Ultimate Practice of Zen Meditation
- White Noise – Sounds to Improve Your Concentration and Relaxation
- Quan Yin Mantra – Collection of QuanYin Healing Mantras
- Medicine Buddha Mantra – Healing of Physical Illnesses and Purification of Negative Karma
- Support Center
- Download Android Blog
Android IllegalArgumentException: A Java exception which should be deliberately thrown by methods that don't like their parameters. It extends RuntimeException, which means it does not need to be caught. The singular name notwithstanding, can represent unsatisfied constraint between more parameters. The more you use and check the parameters, the more you move towards exception in the method invocation proper.
|I have a simple setter method for a Java property and null is not appropriate for this particular property. I have always been torn, in this situation: should I throw an IllegalArgumentException, or a NullPointerException? From the javadocs, both seem appropriate. Is there some kind of understood standard? Or is this just one of those things that you should do whatever you prefer and both is really correct?|
You should be using
First, the NPE JavaDoc explicitly lists the cases where NPE is appropriate. Notice that all of them are thrown by the runtime when
Second, when you see an NPE in a stack trace, what do you assume? Probably that someone dereferenced a null. When you see IAE, you assume the caller of the method at the top of the stack passed in an illegal value. Again, the latter assumption is true, the former is misleading.
Third, since IAE is clearly designed for validating parameters, you have to assume it as the default choice of exception, so why would you choose NPE instead? Certainly not for different behavior -- do you really expect calling code to catch NPE's separately from IAE and do something different as a result? Are you trying to communicate a more specific error message? But you can do that in the exception message text anyway, as you should for all other incorrect parameters.
Fourth, all other incorrect parameter data will be IAE, so why not be consistent? Why is it that an illegal
Finally, I accept the argument given by other answers that parts of the Java API use NPE in this manner. However, the Java API is inconsistent with everything from exception types to naming conventions, so I think just blindly copying (your favorite part) of the Java API isn't a good enough argument to trump these other considerations.
Android IllegalArgumentException is administrated by DownloadAndroid.Info, copyright is reserved.