Anonymous user
Assertions: Difference between revisions
→{{header|Java}}: consistent formatting
(→{{header|Java}}: consistent formatting) |
|||
Line 618:
=={{header|Java}}==
<lang java5>public
int a;▼
public static void main(String[] args) {
▲ int a = 13;
assert a == 42;//throws an AssertionError when a is not 42▼
assert a == 42 : "Error message"; //throws an AssertionError ▼
//the error message can be any non-void expression▼
assert a == 42;
// Throws an AssertionError when a is not 42,
// with "Error message" for the message.
}
}</lang>
Note: assertion checking is disabled by default when you run your program with the <tt>java</tt> command. You must provide the <tt>-ea</tt> (short for <tt>-enableassertions</tt>) flag in order to enable them.
|