There are two ways to perform internal logical checks in Java:
What are the differences between the two methods above (performance, programming flexibility, etc.? Which one is considered good programming practice?
, assert , , ( -ea java, ). , new AssertionError() .
assert
java
new AssertionError()
:
# 1 "assert (x > y);" JVM, . , , , .
# 2 ", (y > x) AssertionError();" , assert-param. .
, "" ( , ), (.. ) / (, param not null). , .
null
assert (x > y) , AssertionError ( ). Java , . , AssertionError, , .
Unfulfilled answers answer my second question (which one is considered good programming practice?). Therefore, I talked about this with one of my friends and, according to him, if some thing is private to the package, and you are the only one who calls this function (for example, private functions), then use assert. If something is publicly available, and you expect some other third-party developer to directly call this function, then do an explicit if check and throw an exception.