Never use assertions to validate arguments of public methods. The Java Language Specification, §14.10, "The assert
Statement" [JLS 20052015], states that
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 A secondary problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime run-time exception (such as
IllegalArgumentException
,IndexOutOfBoundsException
, orNullPointerException
). An assertion failure will not throw an appropriate exception.
...
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 |
...