Assert (false) vs RuntimeException?

I am reading the source of XWalkUIClientInternal and I came across the following code:

switch(type) { case JAVASCRIPT_ALERT: return onJsAlert(view, url, message, result); case JAVASCRIPT_CONFIRM: return onJsConfirm(view, url, message, result); case JAVASCRIPT_PROMPT: return onJsPrompt(view, url, message, defaultValue, result); case JAVASCRIPT_BEFOREUNLOAD: // Reuse onJsConfirm to show the dialog. return onJsConfirm(view, url, message, result); default: break; } assert(false); return false; 

I have never seen this technique and never thought about it before, but I assume that it essentially means โ€œthis is an unreachable code and should never happenโ€, and the failure of the application does not matter. Although technically you can do it with Throwable if it is not caught.

So my question is: which one is better and why, assert(false) or throw a RuntimeException or, possibly, Error ?

+6
source share
2 answers

The biggest difference between

 assert false; 

(Brackets are not needed; assert not a function, but an expression.) And

 throw new RuntimeException(); 

lies in the fact that this statement can be turned off. In fact, it is disabled by default if the JVM is not running with the -ea flag ("enable assertions"). If assertions are included, assert false unconditionally throw an AssertionError that comes from Error . But since claims can be disabled, there are two problems:

  • the error may go unnoticed and
  • Parsing a control stream requires a return dummy return after assert (which is mostly messy).

Therefore, in the above case, I would certainly go with explicit (and shorter)

 throw new AssertionError("invalid type " + type); 

instead of assert followed by a return dummy.

As mentioned in the comments, it is assumed that type is an internal parameter, and an invalid value indicates an error in the logic itself. If it is an input parameter, it should be checked in accordance with normal rules and an IllegalArgumentException should be thrown if the check is not completed.

+9
source

Following Oracleโ€™s recommendations (assertion programming) , assertions were developed for testing purposes:

A statement is a statement in the Java programming language that allows you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might argue that the calculated speed is less than the speed of light.

Each statement contains a Boolean expression, which, in your opinion, will be true when the statement is executed. If this is not the case, the system will throw an error. By verifying that the Boolean expression is indeed true, the statement confirms your assumptions about the behavior of your program, increasing your confidence that the program is error free.

In your example, the developer suggested that the code never reaches the assert . If, in rare cases, this happens, assert(false) throw an Error (since it should never get there). This was done for testing purposes. So, use the statement for testing purposes only.

+1
source

Source: https://habr.com/ru/post/982387/


All Articles