GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Unreachable Code Error in Java - GeeksforGeeks Unreachable Code Error in JavaLast Updated : 11 Sep, 2026 Unreachable code refers to statements that the compiler determines can never be executed during program execution. Such statements usually occur when a control-flow statement, such as return, break, continue, or throw, prevents execution from reaching the following code.
Common Cases Of Unreachable CodeUnreachable code occurs when a control-flow statement prevents the program from reaching a subsequent statement. Common cases include statements placed after return, break, continue, or throw. 1. Unreachable Code After returnThe return statement terminates the execution of a method and returns control to the caller. Therefore, any statement written after an unconditional return statement cannot be executed.
Output: ./ReturnDemo.java:10: error: unreachable statement Explanation: The return statement ends the execution of the main() method. Therefore, the System.out.println() statement placed after return can never be executed. The Java compiler detects this and reports an unreachable statement error. 2. Unreachable Code After breakThe
Output: ./BreakDemo.java:10: error: unreachable statement Explanation: The break statement immediately exits the for loop. As a result, the System.out.println() statement after break can never execute within that iteration. The compiler therefore reports it as an unreachable statement. 3. Unreachable Code After throwThe throw statement is used to explicitly throw an exception. Once a throw statement is executed, control leaves the current flow and the statements immediately following it are not executed.
Output: ./ThrowDemo.java:9: error: unreachable statement Explanation: The throw statement throws an exception before the System.out.println() statement can be reached. Control is transferred to the appropriate catch block. Therefore, the statement following throw is unreachable and causes a compile-time error. 4. Unreachable Code After continueThe continue statement skips the remaining statements in the current loop iteration and transfers control to the next iteration of the loop. Therefore, a statement immediately following an unconditional continue is unreachable.
Output: ./ContinueDemo.java:10: error: unreachable statement Explanation: The continue statement skips the rest of the current loop iteration and moves to the next iteration. Since the System.out.println() statement is placed immediately after an unconditional continue, it can never be executed. The compiler reports it as an unreachable statement. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |