Flash cards
Review the key moves
What is the main idea behind Java assert Keyword?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
public ___ Main {Put the learning moves in the order that makes the concept easiest to apply.
❮ Java Keywords
Example
public class Main {
public static void main(String[] args) {
// Enable assertions ClassLoader loader = ClassLoader.getSystemClassLoader(); loader.setDefaultAssertionStatus(true); // Run the assert example AssertExample example = new AssertExample(); example.run();
}
}
class AssertExample {
public void run() {
int a = 12;
try {
assert a == 12; // Assertion without a fail message
assert a == 12 : "a is not 12";
assert a == 15 : "a is not 15";
} catch (AssertionError e) {
System.out.println(e.getMessage());
}
}
}Definition and Usage
The assert keyword evaluates a boolean expression and throws an AssertionError exception if the expression evaluates to false . When the exception is thrown we say that the assertion failed.
An optional expression can be added which will be used as the exception message if the assertion fails.
Assertions are disabled by default. assert statements are ignored unless assertions are enabled.
The purpose of assertions is to clearly mark where a program is doing something unintended when debugging and testing a program.
❮ Java Keywords