Never use assertions to validate arguments of public methods. According to the Java Language Specification , §14.10, "The assert
Statement" [JLS 2005]:
...assertions should not be used for argument-checking in public methods. Argument-checking is typically part of the contract of a method, and this contract must be upheld whether assertions are enabled or disabled.
Another problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime exception (such as
IllegalArgumentException
,IndexOutOfBoundsException
orNullPointerException
). An assertion failure will not throw an appropriate exception.
...
The assert
statement is supported on the Dalvik VM but is ignored under the default configuration. Assertions may be enabled by setting the system property "debug.assert
" via: adb shell setprop debug.assert 1
or by sending the command line argument "--enable-assert
" to the Dalvik VM.
Bibliography
Item 7. My assertions are not gratuitous | |
[ESA 2005] | Rule 68. Explicitly check method parameters for validity, and throw an adequate exception in case they are not valid. Do not use the assert statement for this purpose |
[JLS 2005] | §14.10, The |
...