Assertions: Difference between revisions

→‎{{header|Java}}: consistent formatting
(→‎{{header|Java}}: consistent formatting)
Line 618:
 
=={{header|Java}}==
<lang java5>public staticclass voidAssertions main(String[] args){
 
int a;
public static void main(String[] args) {
//...input or change a here
int a = 13;
assert a == 42;//throws an AssertionError when a is not 42
 
assert a == 42 : "Error message"; //throws an AssertionError
//when a is not 42 with... "Errorsome message"real forcode thehere message...
 
//the error message can be any non-void expression
assert a == 42;
assert a == 42; //throws Throws an AssertionError when a is not 42.
 
assert a == 42 : "Error message"; //throws an AssertionError
// Throws an AssertionError when a is not 42,
// with "Error message" for the message.
//the The error message can be any non-void expression.
}
}</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.