Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Argument Validation and Using IllegalArgumentException

807580Mar 11 2010 — edited Mar 11 2010
I'd like to know what some of you think of using java.lang.IllegalArgumentException.

Per the javadocs, IllegalArgumentException is "Thrown to indicate that a method has been passed an illegal or inappropriate argument.".

And on a more general note, under what circumstances should we make our methods validate arguments?

Limiting our discussion to methods that can have invalid argument values, I've recently been following the general philosophy that argument validation should be performed on any public methods, but at the same time, I hate littering the code with validation. Perhaps it is sufficient to note the range of valid values in the javadocs for such methods. Getting back to the first question, if we are validating an argument, is throwing an IllegalArgumentException a generally accepted practice when the value is invalid? Or should it throw some application-specific exception? Or, having noted what the valid values are in the javadocs, do you just let it return a bad result, or throw whatever exception it might would throw? With an application-specific exception, the caller is forced to handle the exception, but the caller should not have been passing invalid values to begin with.

Thoughts?

Thanks for sharing.

Edited by: Skotty on Mar 11, 2010 3:35 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 8 2010
Added on Mar 11 2010
4 comments
635 views