Merlin's Magic: Using assertions

Source: Internet
Author: User
Tags assert exception handling expression

Assertion functionality is a new and eagerly awaited feature of the Java 1.4 release. You can consider this feature as an advanced form of exception handling. Assertions are represented as Boolean expressions, and programmers believe that the value of the expression is true at a particular point in the program. For example, a programmer might include assertions in a program so that you never reach the default condition in a switch statement.

Because assertion validation can be enabled or disabled at any time, you can enable assertion validation at test time and disable assertion validation at deployment time. Also, after the program is put into operation, the end user can re-enable the assertion when they encounter a problem.

In this column, I'll tell you a little bit about the basics of adding assertions to your code by demonstrating two different ways to enable and disable assertions to optimize code testing and performance.

Assertion keyword

Assertion checking is like exception handling, but when an assertion fails, the exception is not thrown, and a assertionerror is thrown. As with all errors, it is difficult to recover from an assertion failure, if not impossible, because the failure indicates that the program state has deviated from the normal operating parameters.

The two basic constructs for using assertions are as follows:

assert booleanExpression;
assert booleanExpression : message;

Note that the optional messages (message) will be displayed as the result of the error thrown.

You can add an assertion statement as a precondition or a post condition for a method. You can also establish a Boolean expression to invoke a method. Similarly, you might want to add an assertion to an else condition in an if block, where the condition is already set, or to add an assertion to the default in the switch statement so that the default is never possible. The only limitation of the ASSERT keyword is that it must be in an executable block. An assertion cannot be used with a class variable declaration of an instance, but it can be placed in any method.

Enable assertion

If you want to use assertions, you must use specific command-line options to compile and run the program. Because the compiler is running in 1.3 compatibility mode, you must explicitly require that you run in 1.4 mode. To compile a program with assertions, simply pass the-source 1.4 setting to the Java compiler. By default, assertion checking is disabled, so you should also explicitly enable this feature at run time. Use the-enableassertions option, or the shorter-ea option, to enable assertion checking.

The following simple example introduces you to some of the necessary steps to add an assertion check to your program. It checks the number of command-line arguments and, if the value is not 0 o'clock, reports the problem:

public class AssertTest {
  public static void main(String args[]) {
   assert args.length == 0 : args.length + " != 0";
   System.out.println(args.length);
  }
}

Make sure to compile the program using the-source option as follows:

Javac-source 1.4 Asserttest.java

To test the program, simply run the program and pass the command-line arguments:

Java asserttest 1 2 3 4

Because the program is used to display the number of parameters, the test program will display four. Because the assertion check is disabled by default, no assertion errors are detected.

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.