Alex Rivera | Logout

Java: How to @SuppressWarnings unreachable code?

Asked 2011-05-17T01:33:05.917
12

Sometimes when you are debugging, you have unreachable code fragment. Is there anyway to suppress the warning?

Edit
Report

2 Answers

11

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

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

Likewise, if you are temporarily inserting an early termination for debugging, you might do it like so:

if(true) { return blah; }

or

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

And note that the Java specification explicitly declares that constantly false conditional blocks are removed at compile time, and IIRC, constantly true ones have the condition removed. This includes such as:

public class Debug
{
static public final boolean ON=false;
}

...

if(Debug.ON) {
    ...
    }
answered 2011-05-17T01:42:10.107
6

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

If you're using Eclipse, try @SuppressWarnings("unused").

answered 2011-05-17T01:36:48.807

Your Answer