Shit? Happens, part II (Cai Xueu)

xiaoxiao2021-03-06  41

I mentioned a Shit?, I mentioned the RuntimeException called Shit?, I used it to help identify the procedures that I have caused by negligence, but this way is a bit trouble, and there are some accompanying. The problem. The disadvantage of using Shit? Exception includes:

The programmer should generate and throw a shit? Object.

When this problem occurs, it is usually a very serious situation, should not be Runtime Exception, should be an error.

Before the product is delivered to the customer, the programmer must manually remove all these inspections in the program code, otherwise there is a bad impact on the execution efficiency.

Java? 2 SDK 1.4 new Assertion feature, like Shit? Exception's purpose, but you can freely go to SHIT® Exception. So I have begun to use Assertion, and I will not use Shit? Exception.

You can insert an Assertion narrative in any place in the Java? Program Method. The ASSERTION Narrative method is to add a Boolean judgment after the keyword (KeyWord), and then end this narrative. As shown in the following example:

askERT SIZE () == 0;

If you use the Assertion in the program, you must add the "-Source 1.4" option to tell the Javac compiler, otherwise the Javac preset is not supported assertion syntax. When executed, the "-ea" option is set to tell the JRE to execute the assertion. When the program is executed, if the Boolean judgment is not established, the system will throw an incorrect object of AssertionError. If you don't have to "-ea" option, you will not be executed when you execute.

Assertion looks magical, in fact, the bones are mediocre. With the compiler and Class Loader, you can reach the support of Assertion, which does not require JVM to add OPCode in the instruction set. For example, "Assert size () == 0;" This narrative will be compiled as the following program code as the compiler:

IF (ea) {

IF (size ()! = 0) {

Throw new assertionerror ();

}

}

The compiler and will insert a private static synthetic field similar to the following Class: Private Static Synthetic Field:

Private static boolean elen ea = false;

Because the default value of EA (meaning enable assertion is false, the preset is not executing assertion. If you execute through the "-ea" option, Class Loader will set EA to True when loading this class, so you can execute Assertion.

When executing the program, if the Assertion function is turned on, the efficiency is made slightly poor, and no matter if there is any "-ea" to open the assertion function, the assertion program code will always exist in Bytecode, which will cause volume increase Big, this is the use of Assertions to pay, so use Assertion's timing in the program, it is still necessary to carefully. Assertion can be a good de fault helper as long as it is used. Trademark Name:

Java? Is a trademark of sun microsystems.

Shit? Is a trademark of jerry tsai.

转载请注明原文地址:https://www.9cbs.com/read-72994.html

New Post(0)