Sunday, August 3, 2014

Unreachable Statements in Java

There are many instances where the Java compiler will throw an "unreachable statement" error.  Here are a few that I've found.  If you know of any others that I have missed that don't match any of the ones listed below please tell me about it in the comment section and I might add it.

After Return Statement

public void fooBar() {
   System.out.println("before return");
   return;

   System.out.println("after return"); // unreachable statement
}

Break Statement Inside Both Try and Catch Blocks

Loops

for(int i = 0; i < 27; i++) {
   try {
      System.out.println("inside try block");
      break;
   }
   catch(IllegalArgumentException illegalArgument) {
      System.out.println("inside catch block");
      break;
   }

   System.out.println("after try-catch");  // unreachable statement
}

Switch Statement

switch (27) {
   default:
      try {
         System.out.println("inside try block");
         break;               
      }
      catch(IllegalArgumentException illegalArgument) {
         System.out.println("inside catch block");
         break;
      }

      System.out.println("after try-catch"); // Unreachable statement
      break;
}

Break Statement Inside Finally Block

Loops

for(int i = 0; i < 27; i++) {
   try {
      System.out.println("inside try block");
   }
   finally {
      System.out.println("inside finally block");
      break;
   }

   System.out.println("after try-finally");  // unreachable statement
}

Switch Block

switch (27) {
   default:
      try {
         System.out.println("inside try block");
      }
      finally {
         System.out.println("inside finally block");
         break;
      }

      System.out.println("after try-finally"); // Unreachable statement
      break;
}

After Continue Statement

do {
   System.out.println("before continue");

   continue;

   System.out.println("after continue"); // unreachable statement
} while(true);

Continue Statement Inside Both Try-Catch Blocks

do {
   System.out.println("inside do-while");

   try {
      System.out.println("inside try block");
      continue;
   }
   catch(IllegalArgumentException illegalArgument) {
      System.out.println("inside catch block");
      continue;
   }

   System.out.println("after try-catch"); // unreachable statement
} while(true);

Continue Statement Inside Finally Block

do {
   System.out.println("inside do-while");

   try {
      System.out.println("inside try block");
   }
   finally {
      System.out.println("inside finally block");
      continue;
   }

   System.out.println("after try-finally"); // unreachable statement
} while(true);

While-Loop Whose Boolean Expression Is False

while(false) {
   System.out.println("inside while"); // unreachable statement
}

Infinite Loops

While-Loop

while(true) {
   System.out.println("inside while");
}

System.out.println("after while"); // unreachable statement

Do-While Loop

do {
   System.out.println("inside while");
} while(true);

System.out.println("after do-while"); // unreachable statement

For Loop

for(;;) {
   System.out.println("inside loop");
}
  
System.out.println("after for loop"); // unreachable statement
References

No comments:

Post a Comment