Java Reference
In-Depth Information
i++;
assert i < max;
12.8.1. The
assert
Statement
The syntax for an assertion is:
assert
eval-expr
[:
detail-expr
];
where
eval-expr
is either a
boolean
or
Boolean
expression and
detail-expr
is an optional expression that will be passed to the
AssertionError
con-
structor to help describe the problem. The detail expression is optional;
if present, it provides information to a person who looks at the state-
ment or thrown exception. If the detail is a
Throwable
it will become
the cause of the
AssertionError
; that is, it will be the value returned by
the error's
getCause
method. Otherwise, the detail will be converted to a
string and become the detail message for the error.
When an
assert
is encountered in the code, the first expression is evalu-
ated. If the resulting value is
true
, the assertion passes. If it is
false
, the
assertion fails, and an
AssertionError
will be constructed and thrown.