Android IllegalArgumentException


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 IllegalArgumentException (IAE), not NullPointerException (NPE) for the following reasons:

First, the NPE JavaDoc explicitly lists the cases where NPE is appropriate. Notice that all of them are thrown by the runtime when null is used inappropriately. In contrast, the IAE JavaDoc couldn't be more clear: "Thrown to indicate that a method has been passed an illegal or inappropriate argument." Yup, that's you!

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 null is so special that it deserves a separate exception from all other types of illegal arguments?

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.

Home -> Error Message -> IllegalArgumentException

Related post

Android is slow
Free ram optimizer
RAM memory optimizer
RAM booster full
ram booster download
Mobile ram booster
How to make android faster
How to increase android ram
Max android ram optimizer software
How to increase android memory
ram optimizer software
Android ram manager applications
Android memory process
Android internal memory
Android app memory
Android memory optimization
Android memory card
Android memory optimizer review
How much android ram
Android memory booster review
Best memory app for android
Apps on android
Att wireless
Apps for the android
htc magic
apps for htc
android software development kit
application android
update for android
android sync
memory card
memory leakage
memory leaking
memory fix
google maps mobile
android free
android apps downloads
download android apps
free android downloads
free android games
Android hero
Android error
Android problem
Android java.lang.SecurityException
T mobile g 1
android apps free
htc con android
droid apps
android program
battery drains quickly
How to improve android battery
Improve android battery life
Tips for saving battery
Free battery apps for android
Android apk
apk for android
Download tomcat apk
Talking tom apk for android
Angry birds android apk
Memory booster apk
Android ram boosting tool
Android ram app
Android 2.2 ram
Android ram hack
Galaxy s ram
Droid 2 ram
Facebook for android force close
Android market download unsuccessful error
Error android
Android server error
Android sync error
Facebook android error
Droid apps free
Best droid apps
Free android apps
App for droid
Free app download
top android apps
Battery droid X
DROID X Battery Life
Droid x battery issues
Droid battery life problems
Battery life of droid
Battery life on htc evo
Android batterybooster
Android memorybooster
Android ram freeware
Alchemy android
Android themes for free
talking tom android
talking tomcat download
talking tom download
ADW launcher apk
Android 2.1 update
Android theme chrome

Android operating system
Get more ram on android
linux memory usage
Android linux memory
Android phone memory
Android app killer
Android system manager
Android process kill
Android memory booster
Galaxy s ram
htc evo memory
Android memory management
Android memory optimizer software
How to find android ram
Improve android memory
Memory booster freeware
memorybooster android
Memory booster apk download
Android ram hack
Android speed up
Android memory cleaner
Android memory usage
Motorola droid ram
Android g3
Android mp3
Mobile themes download
Android free apps
Android update
Free downloadable ringtones
Android themes free download
Free Ringtones Download
Tomcat App
Android gps
Android free theme download
Mobile Application Development
Android Themes Download
HTC Touch applications
Android review
Android develop
How to android app
Android free software
Best app for android
Android application
Google Android App
Android news and updates
Free Android Software
Droid applications
Free Android Themes
Android App Download
Android camera app
Android dragonball z
Android free applications
Android how to
Android jailbreak
Htc g2
G2 android
Free mobile softwares
Free softwares for mobile
Apps for droid
Memorybooster for droid
Android free memory app
Android app memory
Android memory optimization
Optimize android memory
Battery life on droid
Droid app killer
Battery drains on droid
Angry birds android download
Free themes for android
Swype beta download
htc upgrade
htc touch reviews
apps for android
Droid t2
Android 2.0 error message
Android force close error
Network error android
Android IllegalMonitorStateException
Apps for android free
Best free apps for android
Top droid apps
Free htc apps
Android apps 2010
Android games
Android free games
Free software for mobile
Mobile software download
Battery life android
HTC android battery
Android app battery
Best android battery app
Fifa 10 android
Fifa 10 android download
Download app inventor
Free android panda home download
Nexus internal memory