Rough Java assertion assert initial use: Assert Open, assert usage

Source: Internet
Author: User

Reference documents: http://blog.sina.com.cn/s/blog_59c9412d0100fd55.html

1 description

Java assertion assert is introduced by jdk1.4.

The JVM assertion is turned off by default.

Assertions can be locally opened, such as: The parent class prohibits assertions, and subclasses open assertions, so it is generally said that " assertions do not inherit ."

 Assertions only apply to complex modal processes.

  Assertions are generally used to judge the execution structure of a program, and never allow assertions to handle business processes.

2 Determine if eclipse has an assertion turned on

The code is as follows:

     Public Static void Main (String args[]) {        booleanfalse;         // If an assertion is turned on, the value of IsOpen is changed to True        assert true ;         // whether the assertion is turned on for printing, or false if the assertion is not enabled         System.out.println (isOpen);    }

3 Opening and closing assertions in eclipse

Select menu: Run-and-run configurations

Open Assertion:-ea

Close assertion:-da, or delete-ea

3 Assertion Use

3.1 First method of use

    /**      * How to use assertions one      */public     staticvoid  useAssertExt1 (        {boolean isOk = 1 > 2;         assert isOk;        System.out.println ("normal program");    }

The results of the implementation are as follows:

Exception in thread "main" Java.lang.AssertionError
At Com.rollercoaster.codecomplete.AboutAssertion.useAssertExt1 (aboutassertion.java:43)
At Com.rollercoaster.codecomplete.AboutAssertion.main (aboutassertion.java:16)

Description

Apparently isOK is false, so throw Assertionerror

3.2 Second method of use

    /**      *     The first example of an assertion * /public     staticvoid  useAssertExt2 () {         Boolean isOk = 1 > 2;         Try {            assert isOk: "program error";            System.out.println ("normal procedure");         Catch (Assertionerror err) {            System.out.println (err.getmessage ());        }    }        

The results of the operation are as follows:

Program error

Description

The assert is followed by a colon expression. If the smoke is true before the colon is ignored, otherwise thrown assertionerror, the error content is the content after the colon.

Press F3 to view the source code, as below, you can see that Assertionerror is inherited from error, not exception, so the catch part with exception is not able to capture assertionerror information.

(Version 1.7:51.0, super bit)
public class Java.lang.AssertionError extends Java.lang.Error {...

5 more arguments about assertions (reprint):

-ea Java-ea Open assertion for all user classes
-da java-da turn off assertion
-ea:<classname> Java-ea:myclass1 for all user classes Open MyClass1 Assertion
-da:<classname> java-da:myclass1 close MyClass1 Assertion
-ea:<packagename> JAVA-EA:PKG1 Open PKG1 Package Assertion
-da:<packagename> java-da:pkg1 close PKG1 Package assertion
-ea: ... java-ea Open the default package (Nameless package) assertion
-da: ... java-da: ... Close the default package (Nameless package) assertion
-ea:<packagename> ... java-ea:pkg1 ... Open the PKG1 package and its child package assertion
-da:<packagename> ... java-da:pkg1 ... Close the PKG1 package and its child packages assertion
-esa Java-esa Open the System class assertion
-DSA JAVA-DSA shut down the system class assertion
Synthetic use JAVA-DSA:MYCLASS1: PKG1 closing assertion

for MYCLASS1 and PKG1 packages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.