Java Reference
In-Depth Information
12.10. Turning Assertions On and Off
By default, assertion evaluation is turned off. You can turn on all asser-
tion evaluation in a virtual machine, or on a package, class, or class load-
er basis. Because they can be on or off, you must be careful to avoid side
effects that affect non-assertion code.
You control assertion evaluation either by passing command-line options
to the virtual machinedescribed shortlyor by using methods provided by
the class loadersee " Controlling Assertions at Runtime " on page 444 .
12.10.1. Why Turn Assertions On and Off?
The first question to answer is why you would want to be able to switch
assertion evaluation on and off. The usual example is to turn assertions
on during development, but turn them off when your system is shipping.
The argument is that you will have caught any insanities during the de-
velopment cycle so the overhead of doing all the checks is not worth the
effort.
This is tempting logic, but you can open yourself up to serious problems.
The point of assertions is to catch bugs early before they corrupt things
and cause bizarre side effects. When someone else is running your code,
tracking down causes for problems is much more difficult, so catching
them early and reporting them clearly is much more important, even if
it should happen a lot less often. You should only turn off assertions in
code where they have very noticeable performance effects. In those crit-
ical sections of code, though, you will want to turn them off.
12.10.2. Controlling Assertions on the Command Line
Assertion evaluation is off by default. To change this, you can use stand-
ard command line options if you are using a command-line-driven virtual
machine:
 
Search WWH ::




Custom Search