Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to ignore the 'Unreachable statement' error?

Tags:

java

Is it possible to somehow ignore this error? I find it much easier to just put return in front of the code I don't want to run than to comment it (when the comments overlap and behave badly)...

like image 596
Martin Melka Avatar asked Jan 31 '12 10:01

Martin Melka


People also ask

How do you solve an unreachable statement?

If you keep any statements after the return statement those statements are unreachable statements by the controller. By using return statement we are telling control should go back to its caller explicitly .

Why is my statement unreachable?

The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.

What does unreachable code error mean?

Unreachable code error occurs when the code can't be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.


2 Answers

No. It's a compile time error. So you must get rid of it before running your class.

What I usually do is put a fake if statement in front of it. Something like:

if(true)     return; // unwanted code follows. no errors. i++; j++; 

With this code, you will not get a Unreachable statement error. And you will get what you want.

like image 92
Pablo Santa Cruz Avatar answered Sep 30 '22 16:09

Pablo Santa Cruz


33. if (1==1) return; 34. System.out.println("Hello world!"); 

It works in other languages too. But ByteCode without row 34.

like image 24
Dmitry Sokolyuk Avatar answered Sep 30 '22 16:09

Dmitry Sokolyuk