Java: How is @SuppressWarnings unreachable code?

Sometimes when you debug, you have an unreachable piece of code. Is there a way to suppress a warning?

+6
source share
4 answers

The only way to do this on any compiler is @SupressWarnings("all") .

If you use Eclipse, try @SuppressWarnings("unused") .

+4
source

Java has (primitive) debugging support like this in that simple if in boolean constants will not generate such warnings (and, indeed, when the evaluation is false, the compiler will delete the entire conditional block). So you can do:

 if(false) { // code you don't want to run } 

Similarly, if you temporarily insert early termination for debugging, you can do it like this:

 if(true) { return blah; } 

or

 if(true) { throw new RuntimeException("Blow Up!"); } 

And note that the Java specification explicitly states that, at compile time, permanently false conditional blocks are deleted, and IIRC, constantly true, have the condition removed. These include:

 public class Debug { static public final boolean ON=false; } ... if(Debug.ON) { ... } 
+9
source

As Cletus tells us ,

It depends on your IDE or compiler.

However, at least for Eclipse, there is no way to do this. With my Eclipse configuration, unreachable code causes a compile-time error, not just a warning. Also note that this is different from β€œdead code,” for example,

 if (false) { // dead code here } 

for which Eclipse (by default) gives a warning, not an error.

+3
source

According to Java Language Specification :

This is a compile-time error if the statement cannot be executed because it is not available.

Sometimes you can turn unreachable code into dead code (for example, the body of if (false) {...} ). But this error is part of the definition of language.

+3
source

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


All Articles