Header Ads

  • Breaking Now

    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.

    Post Top Ad

    Post Bottom Ad