Skip to content
On this page

Use Assertions

Assertion is a way of debugging a program. In Java, assertions are implemented using the assert keyword.

Let’s look at an example first:

java
public static void main(String[] args) {
  double x = Math.abs(-123.45);
  assert x >= 0;
  System.out.println(x);
}

The statement assert x >= 0; is an assertion, and the assertion condition x >= 0 is expected to be true . If the evaluation result is false , the assertion fails and AssertionError is thrown.

When using the assert statement, you can also add an optional assertion message:

java
assert x >= 0 : "x must >= 0";

In this way, when the assertion fails, AssertionError will bring the message x must >= 0 , which is more convenient for debugging.

The characteristic of Java assertions is that AssertionError will be thrown when the assertion fails, causing the program to end and exit. Therefore, assertions cannot be used for recoverable program errors and should only be used during development and testing phases.

Assertions should not be used for recoverable program errors. For example:

java
void sort(int[] arr) {
    assert arr != null;
}

The exception should be thrown and caught at the upper level:

java
void sort(int[] arr) {
    if (arr == null) {
        throw new IllegalArgumentException("array cannot be null");
    }
}

When we use assert in a program, for example, a simple assertion:

java
public class Main {
    public static void main(String[] args) {
        int x = -1;
        assert x > 0;
        System.out.println(x);
    }
}

Assert that x must be greater than 0 In fact, x is -1 , and the assertion will definitely fail. After executing the above code, we found that the program did not throw AssertionError , but printed the value of x normally.

What's going on? Why doesn't the assert statement work?

This is because the JVM turns off assertion instructions by default, that is, when encountering an assert statement, it automatically ignores it and does not execute it.

To execute the assert statement, -enableassertions (can be abbreviated as -ea ) parameter must be passed to the Java virtual machine to enable assertions. Therefore, the above program must be run under the command line to be effective:

sh
$ java -ea Main.java
Exception in thread "main" java.lang.AssertionError
	at Main.main(Main.java:5)

You can also selectively enable assertions for specific classes. The command line parameters are: -ea:com.itranswarp.sample.Main , indicating that assertions are only enabled for com.itranswarp.sample.Main class.

Or enable assertions for a specific package. The command line parameter is: -ea:com.itranswarp.sample... (note that there are 3 . at the end), which means to enable assertions for the com.itranswarp.sample package.

In actual development, assertions are rarely used. A better way is to write unit tests. We will explain the use of JUnit later.

Summary

Assertion is a debugging method. If the assertion fails, AssertionError will be thrown. Assertion can only be enabled during the development and testing phases;

Assertions cannot be used for recoverable errors, but exceptions should be thrown;

Assertions are rarely used and a better approach is to write unit tests.

Use Assertions has loaded