[Java] Using assertions in exception handling

use assertion

assert( Assertion) is a way of debugging a program. In Java, use assertkeywords to implement assertions.

Let's look at an example first:

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

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

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

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 assertion is: when the assertion fails, it will be thrown AssertionError, causing the program to end and exit. Therefore, assertions cannot be used for recoverable programming errors and should only be used during development and testing.

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

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

Exception should be thrown and caught in upper layer:

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:

// assert
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, the assertion definitely fails. Execute the above code, and found that the program did not throw AssertionError, but printed the value of x normally.

How is this fat four? Why doesn't the assert statement work?

This is because JVMthe assertion instruction is turned off by default, that is, the assert statement is automatically ignored and not executed.

To execute an assert statement, you must pass the -enableassertions` (abbreviated as -ea) parameter to the Java virtual machine to enable assertions. Therefore, the above program must be run on the command line to be effective:

$ 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 parameter is: , which means only enable assertions -ea:com.itranswarp.sample.Mainfor this class.com.itranswarp.sample.Main

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

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

summary

Assertion is a way of debugging. Assertion failure will be thrown AssertionError, and assertion can only be enabled in the development and testing phase;

Recoverable errors cannot be asserted, but exceptions should be thrown;

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

If you have gained something after reading, you can also join the discussion in the Knowledge Planet community:

Guess you like

Origin blog.csdn.net/ihero/article/details/132190132