Questions tagged [unreachable-statement]

Use this tag when your compiler is giving you "Unreachable code detected", "Unreachable statement" warning / error message for your code in question and you don't understand the reason.

Unreachable statements are codes that compilers detect as not reachable. There could be variety of situations when this happens.

Having a return statement in the middle of the code. The code that follows are considered as not reachable.

C# example:

string GetName()
{
   string s = "abc";
   return s; 
   s = s + "d"; // Warning CS0162: Unreachable code detected
   return s;
}

Another, would be having a variable defined with certain value and then check the variable for different value to do some operations.

while (false)
{
   Console.WriteLine("unreachable"); // Warning CS0162: Unreachable code detected
}

Java example:

public String getName()
{
   String s = "abc";
   return s; 
   s = s + "d"; // Unreachable statement
   return s;
}

Compilers generally throw Warning message for these type of issues. But it depends on compiler to compiler. For instance .NET compiler may just throw warning but Java may throw it as error.

61 questions
89
votes
8 answers

Why does Java have an "unreachable statement" compiler error?

I often find when debugging a program it is convenient, (although arguably bad practice) to insert a return statement inside a block of code. I might try something like this in Java .... class Test { public static void main(String args[]) { …
Mike
  • 58,961
  • 76
  • 175
  • 221
59
votes
5 answers

Why does a Java Compiler not produce an unreachable statement error for an unreachable then statement?

If I try to compile for(;;) { } System.out.println("End"); The Java compiler produces an error saying Unreachable statement. But if I add another "unreachable"(according to me) break statement and make it: for(;;) { if(false)…
dryairship
  • 6,022
  • 4
  • 28
  • 54
8
votes
3 answers

I get the error "Unreachable statement" return in android

Why do I get the error that line 92 is an unreachable statement? The error is in this line: final RadioButton r1 = (RadioButton) getView().findViewById(R.id.radio1); Code: public class TabFragmentA extends Fragment { @Override public View…
Edd_boy
  • 103
  • 1
  • 2
  • 6
6
votes
1 answer

Why isn't unreachable code detected when an if condition is a constant?

I'm studying for a Java exam and came across the "unreachable statement" compiler error, e.g: Source.java:10: error: unreachable statement System.out.println("This code is not reachable"); Am trying to understand when this would or wouldn't…
4
votes
1 answer

The code clearly has an unreachable statement in it yet compiles - why?

The statement on Line 2 will never execute but the compiler doesn't complain: class Test { public static void main(String[] args) throws Exception { throwE(); // throw new Exception(); // Line 1 doStuff(); …
Igor Soudakevitch
  • 671
  • 10
  • 19
3
votes
2 answers

Why is it giving me a return missing error?

Why does this method keep telling me it's missing a return statement ? and if I remove the else it tells me the return true isn't reachable. Thanks in advance for your help ! public static boolean Digit(String pass){ for (int i=0; i <…
MohSameh
  • 65
  • 6
3
votes
2 answers

Unreachable Statement in PHP

require_once 'C:/wamp/www/FirstWebsite/CommonFunctions.php'; function SelectRowByIncrementFunc(){ $dbhose = DB_Connect(); $SelectRowByIncrementQuery = "SELECT * FROM trialtable2 ORDER BY ID ASC LIMIT 1"; $result = mysqli_query($dbhose,…
2
votes
2 answers

java - unreachable statement help (linked lists)

So I'm trying to implement a get method for my singly linked list class and I get the error: unreachable statement. I was wondering how can I fix this? public T get(int i) { // TODO: Implement this Node u = head; for(int j = 0; j < i;…
Dobrikella
  • 145
  • 1
  • 11
2
votes
1 answer

How is my simple if-else statement unreachable code?

Honestly am shocked I am getting an error. I'm a junior CS major and I can't get this simple program to work. Clion says these two lines are unreachable, yet my test cases seem to work. Code: #include #include using namespace…
Coder117
  • 801
  • 2
  • 9
  • 22
2
votes
1 answer

Android tutorial switch 1st case method is unreachable

I am doing the android tutorial and in the Adding Actions Buttons part, openSearch() and openSettings() are undefined. So I made them as private voids in the same class. In the switch, though, openSearch(); is apparently unreachable. When I delete…
2
votes
7 answers

Why do I get unreachable statement error in Java?

I'm putting together a code for a hailstone sequence I found in an online tutorial, but in doing so I ran into an unreachable statement error. I don't know if my code is correct and I don't want advice in correcting it if I'm wrong(regarding the…
user3889963
  • 477
  • 3
  • 6
  • 17
2
votes
1 answer

Javacc Unreachable Statement

In my grammar there are production rules for expressions and fragments which originally contained indirect left recursion. This is the rules after I removed the recursion from them. String expression() #Expression : {String number; Token t;} { …
2
votes
2 answers

Unreachable statement error using while loop in java

Possible Duplicate: Why is this code giving an “Unreachable Statement” error? This seems very easy question, I found this question in one book. If anyone help me to figure out why I'm getting error. do { System.out.print("inside…
Ravi
  • 30,829
  • 42
  • 119
  • 173
2
votes
2 answers

"unreachable statement" when trying to compile a "while" statement

I'm new to Java and working through some coursework. However, on the following piece of code I'm getting the error "Unreachable statement" when trying to compile. Any pointers as to what I'm doing wrong? public String getDeliveredList() { int…
1
vote
2 answers

Compiling error - unreachable statement

I'm getting this error: src\server\model\players\Client.java:1089: error: unreachable statement PlayerSave.saveGame(this); ^ Note: Some input files use unchecked or unsafe operations. Note: Recompile…
brain
  • 19
  • 2
1
2 3 4 5