Assert statement contains two part:

assert firstPart : secondPart;

I know firstPart must be a boolean expression or boolean variable. But I have a confusion with secondPart. What it can be?? It may be just an error message as a string? Or it could also be a method? It should only return a String or anything?

Please clarify me the requirements, restrictions and nuances relating to the secondPart of assert statement.

有帮助吗?

解决方案

Slightly paraphrasing from the JavaSE Programming with Assertions tech note, your "secondPart" (referred to as Expression2) "is any expression that has a value. (It cannot be an invocation of a method that is declared void.)"

"The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message."

So "secondPart" can be any expression that has a value; that value is converted to its string form for use in the AssertionError thrown by the assert.

其他提示

The secondPart is the detail message of the assertion.

See the jls, 14.10

AssertStatement:

assert Expression1;
assert Expression1:Expression2;

About Expression2 it says:

If the evaluation completes normally, an AssertionError instance whose "detail message" is the resulting value of Expression2 is created.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top