void A() {
B();
// return; <- compiler error "unreachable code"
if (true) return; // <- this works
// code that I will test later
...
C();
D();
}
This is what I do now. Is there a simple "exit;
" or "return;
" that will not yield "unreachable code"
error without using the if
?
Just to make it clear: if (true) return;
works (with warning "Dead code" which I don't care and can suppress)!
if I simply use return;
only then I get the "unreachable code"
error.
Note: A simple "This can't be done without using if
" is also an acceptable answer, provided a reference.
No, Java does not support an unconditional return in the middle of a code block, since it doesn't "make sense" in the general scheme of things. (There are good reasons that the Java compiler does and must do fairly strong control flow analysis, and if the compiler allowed it the JVM verifier would still reject it.)
The problem is that there is:
if (true) return;
which will always call return, therefore the latter code is unreachable. If you use a real condition there's not going to be this warning.
But if you just want to test part of the method I would suggest just commenting the rest of the method that will be tested later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With