Java Reference
In-Depth Information
Enabling/Disabling Assertions
The goal in using assertions is to detect logic errors in programs. Typically, assertions should be enabled in
development and test environments. Assertions help programmers find the location and type of problems in code
quickly. Once an application is tested, it is very unlikely that the assertions will fail. Java designers kept in mind
the performance penalty that you may incur by using assertions in production environment. This is the reason
that assertions are disabled at runtime by default. Although it is not desirable to enable assertions in a production
environment, you have options to do so.
Java provides command-line options (or switches) to enable assertions at runtime at various levels. For example,
you have options to enable assertions in all user-defined classes, all system classes, all classes in a package and its
subpackages, just for one class, etc. Table 10-1 lists all switches that you can use on the command line to enable/
disable assertions at runtime.
Table 10-1. Command-Line Switches to Enable/Disable Assertions at Runtime
Command-Line Switch
Description
-enableassertions
or
-ea
Used to enable assertions at runtime for system classes as well as user-defined
classes. You can pass an argument to this switch to control the level at which
assertions are enabled.
-disableassertions
or
-da
Used to disable assertions at runtime for system classes as well as user-defined
classes. You can pass an argument to this switch to control the level at which
assertions are disabled.
-enablesystemassertions
or
-esa
Used to enable assertions in all system classes. You cannot pass any arguments to
this switch.
-disablesystemassertions
or
-dsa
Used to disable assertions in all system classes. You cannot pass any arguments to
this switch.
Two switches, -ea and -da , let you control the enabling and disabling of assertions at various levels. You can pass
an argument to these switches to control the level at which assertions should be enabled or disabled. Note that you
cannot pass any arguments to -esa and -dsa switches. They enable and disable assertions in all system classes. If
you pass an argument to the -ea or -da switch, the switch and the argument must be separated by a colon, as shown
below. Table 10-2 lists the possible arguments that can be used with these switches.
-ea:argument
-da:argument
 
Search WWH ::




Custom Search