Assertions are turned on or off at runtime. When disabled, assertions still exist in the class files but are not executed and consume no time. You can enable and disable assertions for an entire application or on a package-by-package or even class-by-class basis. By default, assertions are turned off in Java. To enable them for your code, use the java command flag -ea or -enableassertions:
% java -ea MyApplication
To turn on assertions for a particular class, append the class name:
% java -ea:com.allwareshare.examples.Myclass MyApplication
To turn on assertions just for particular packages, append the package name with trailing
ellipses (. . .):
% java -ea:com.allwareshare.examples...MyApplication
When you enable assertions for a package, Java also enables all subordinate package
names (e.g., com.allwareshare.examples.text). However, you can be more selective by using
the corresponding -da or -disableassertions flag to negate individual packages
or classes. You can combine all this to achieve arbitrary groupings like this:
% java -ea:com.allwareshare.examples...
-da:com.allwareshare.examples.text-ea:com.allwareshare.examples.text.MonkeyTypewriters
MyApplication
This example enables assertions for the com.allwareshare.examples package as a whole,
excludes the package com.allwareshare.examples.text, and then turns exceptions on for
just one class, MonkeyTypewriters, in that package.
0 comments:
Post a Comment