What is unreachable statement in java

Unreachable statement using final and non-final variable in Java

Unreachable statements in Java refers to those sets of 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 which are as follows:

  • Have a return statement before them
  • Have an infinite loop before them
  • Execution forcibly terminated before them

Java

Output explanation:

This is because the statement ‘System.out.println(“Unreachable statement”)’, will never be executed and java doesn’t support unreachable statements. Making those variables final also makes them constant variables and in java or any other programming language, the value of the constant variable can’t be changed. So, throughout the life cycle of the program, no condition will be approached to come out of the while loop.
This, in turn, makes the second statement unreachable i.e. JVM can’t reach that statement during the program life cycle. So, it is giving an “unreachable statement” error. Here, the program is right if looked from the coding point of view but what actually JVM thinks is that if in any condition a statement is not executed, then what is the need of using that statement? So, try to avoid this type of situation.

Читайте также:  Удалить из строки все кроме букв и цифр php

What if the above syso statement is commented?

Geek you must be wondering the same what if it is commented out or removed then what will be the expected output will be fuflfiled. Yes you are right, hold and think about it as the required code will also fulfil condition of containing unreachable statement. It is as shown below in the programs as follows:

Источник

How to Fix Unreachable Statement Errors in Java

How to Fix Unreachable Statement Errors in Java

Introduction to Statements and Compile-time Errors in Java

Statements are foundational language constructs that have an effect on the execution of a program. Statements are similar to sentences in natural languages. In Java, there are three main types of statements, namely expression statements, declaration statements, and control-flow statements [1].

As a compiled programming language, Java has an inbuilt mechanism for preventing many source code errors from winding up in executable programs and surfacing in production environments [2]. One such error, related to statements, is the unreachable statement error.

What Causes the Unreachable Statement Error?

By performing semantic data flow analysis, the Java compiler checks that every statement is reachable and makes sure that there exists an execution path from the beginning of a constructor, method, instance initializer, or static initializer that contains the statement, to the statement itself. If it finds a statement for which there is no such path, the compiler raises the unreachable statement error [3].

Unreachable Statement Error Examples

After a branching control-flow statement

The break , continue , and return branching statements allow the flow of execution to jump to a different part of the program. The break statement allows breaking out of a loop, the continue statement skips the current iteration of a loop, and the return statement exits a method and returns the execution flow to where the method was invoked [4]. Any statement that follows immediately after a branching statement is, by default, unreachable.

After break

When the code in Fig. 1(a) is compiled, line 12 raises an unreachable statement error because the break statement exits the for loop and the successive statement cannot be executed. To address this issue, the control flow needs to be restructured and the unreachable statement removed, or moved outside the enclosing block, as shown in Fig. 1(b).

package rollbar; public class UnreachableStatementBreak < public static void main(String. args) < int[] arrayOfInts = ; int searchFor = 12; for (int integer : arrayOfInts) < if (integer == searchFor) < break; System.out.println("Found " + searchFor); >> > >
UnreachableStatementBreak.java:12: error: unreachable statement System.out.println("Found " + searchFor); ^
package rollbar; public class UnreachableStatementBreak < public static void main(String. args) < int[] arrayOfInts = ; int searchFor = 12; boolean found = false; for (int integer : arrayOfInts) < if (integer == searchFor) < found = true; break; >> if (found) < System.out.println("Found " + searchFor); >> >

Rollbar in action

Источник

Unreachable return statement in Java

Apparently my last return false is unreachable but why if my catch block only runs in exceptional cases?

7 Answers 7

Because you have a return statement within the try.

There are only two possible executions paths in your code.
1. The line

Field field = beanClass.getDeclaredField(name); 

. works as expected and the next line returns:

return (field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT 

2. An exception occurs and the return in the catch block executes.

Given those two paths, the third return statement cannot ever be reached.

There is actually a third possible execution path. A subclass of Throwable that isn’t a sublcass of Exception (e.g. an Error) is thrown. It remains uncaught and bubbles up to the calling function, once again missing the third return statement.

Because you’ve also got a return at the end of the try block.

What you have is essentially the equivalent of the following:

if (something) return true; else return false; else return false; 

Now, why would you have two else statements? That’s right, you wouldn’t.

Because you also have a return in your try block, so no matter what, a return is reached within the try catch construct.

While all the above are true, the reasoning is that your code will either process and return successfully via the happy path, or it will throw an exception and be handled accordingly, so in essence, you’ve provided an if/else path of execution. The compiler will never reach the third return statement. If you removed the return false; from the catch block, the warning would go away. You would get the same warning if you were to handle your checked exception in some way (re-throw up the stack) because the code will either return as expected or throw and exception (another if/else path of execution).

Just to reiterate, don’t use try/catch blocks for logical flow. The catch block should be used for handling exceptions/errors gracefully. Secondly, any function that is NOT declared void must return some form of the declared type, in your case a boolean value.

private static boolean isTransientField(String name, Class beanClass) < try < Field field = beanClass.getDeclaredField(name); if((field.getModifiers() & Modifier.TRANSIENT) == Modifier.TRANSIENT) < return true; >else < return false; >> catch (Exception e) < e.printStackTrace(); >> 

You are both right, should have left out the code sample. My point was that whether a exception occurs or not, the function must return a boolean value for the compiler to «not cough.»

Источник

Оцените статью