Basic use of assert in Java

Source: Internet
Author: User

Assertion checks a Boolean expression. A correct program must ensure that the value of this Boolean expression is true. If this value is false, the system will throw assertionerror.

Syntax:

Assert expression1;
Assert expression1: expression2;

Expression1 indicates a Boolean value or expression or a method that can return a Boolean value. expression2 is an object. Generally, a written string is contained in assertionerror, prompting you

For example:

Assert 1! = 1: "--> error! <--";

The running will print:

Exception in thread "Main" Java. Lang. assertionerror: --> error! <--
At com. auscend. kenth. Student. Information. util. Test. Main (test. Java: 5)

To support assert, You need to enable this function at runtime. Java-ea indicates enabling-da indicates disabling the full name "-enableassertions" and "-disenableassertions" indicates disabling the function during release during development and debugging.

Assert helps developers find program error states to improve program robustness, and should not be part of the normal running logic of the program.

Copy search

Copy search

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.