Friday, May 11, 2007

How many forms of assertions we have?

There are two forms of assertions:

The first, simpler form is:

assert Expression1 ;
where Expression1 is a boolean expression.
When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message.
While the second form of the assertion statement is:

assert Expression1 : Expression2 ;

Expression1 is a boolean expression.
Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)

This form is used when the assert statement has to provide a detail message for the AssertionError.The system passes the value of Expression2 to the appropriate AssertionError constructor, and this constructor uses the string representation of the value as the error's detail message. This detail message helps in analysing and diagnosing the assertion failure which ultimately helps in resolving the error.


Rizwan Shaikh on January 5, 2014 at 12:07 AM said...

thanks. It helped me.


Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker