Assertions in general
Assertions in general :
- when an assertion is fired, pass the offending piece of data to AssertionError
- the checks performed inside an assertion are usually simple boolean conditions - side effects are usually to be avoided
- since assertions can be disabled, the behaviour of a program must not depend upon assertions being executed
- if desired, the conditional compilation idiom can still be used
- you can almost always ignore the methods in ClassLoader regarding assertions - they are intended for interpreters such as java, and are not used in a typical program
- at runtime, assertions are disabled by default
- the -enableassertions and -disableassertions runtime parameters (or their synonyms -ea and -da) control assertions to any degree of granularity
- AssertionError behaves as any other Error, and is not affected by enabling or disabling of assertions.
- to compile code containing assertions, older JDKs need to add -source 1.4 as a command line argument to javac
| Runtime command line | Classes with assertions enabled |
|---|---|
| java -ea Main | all classes |
| java -ea:com.Blah Main | one particular class named Blah |
| java -ea:com.data... Main | all classes in the com.data package, and all of its sub packages |
| java -ea -da:com.Blah Main | all but one class named Blah |
See Also :
Would you use this technique?
|
|