Unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.
Questions tagged [unreachable-code]
181 questions
109
votes
9 answers
Unreachable code, but reachable with an exception
This code is part of an application that reads from and writes to an ODBC connected database. It creates a record in the database and then checks if a record has been successfully created, then returning true.
My understanding of control flow is as…

0xCAFEBABE
- 5,576
- 5
- 34
- 59
92
votes
8 answers
Can branches with undefined behavior be assumed unreachable and optimized as dead code?
Consider the following statement:
*((char*)NULL) = 0; //undefined behavior
It clearly invokes undefined behavior. Does the existence of such a statement in a given program mean that the whole program is undefined or that behavior only becomes…

usr
- 168,620
- 35
- 240
- 369
59
votes
1 answer
Why is an if/else if/else for a simple boolean not giving an "unreachable code" error
Why is this code not giving an "unreachable code" error? Since a boolean can only be true or false.
public static void main(String args[]) {
boolean a = false;
if (a == true) {
} else if (a == false) {
} else {
int c = 0;
…

vvill
- 1,100
- 2
- 12
- 21
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
54
votes
8 answers
Unreachable code error vs. dead code warning in Java under Eclipse?
Does anyone know why:
public void foo()
{
System.out.println("Hello");
return;
System.out.println("World!");
}
Would be reported as an "unreachable error" under Eclipse, but
public void foo()
{
System.out.println("Hello");
…

Uri
- 88,451
- 51
- 221
- 321
49
votes
4 answers
Why isn't this code unreachable?
I found a case where I have some code that I believe to be unreachable and is not detected.
No warning is issued neither by the compiler nor by Visual Studio.
Consider this code:
enum Foo { A, B, C }
class Bar { public Foo type; }
static class…

Michele Ippolito
- 686
- 1
- 5
- 12
30
votes
3 answers
if(false) vs. while(false): unreachable code vs. dead code
I tried the following in Eclipse:
if (false) {}: warning 'dead code'
while (false) {}: compilation error 'unreachable code'
I was wondering whether there is a real 'reason' for this difference. I already found this...
Unreachable code compiler…

luukburger
- 637
- 6
- 14
26
votes
5 answers
Emulating GCC's __builtin_unreachable?
I get a whole lot of warnings about switches that only partially covers the range of an enumeration switched over. Therefor, I would like to have a "default" for all those switches and put __builtin_unreachable (GCC builtin) in that case, so that…

Johannes Schaub - litb
- 496,577
- 130
- 894
- 1,212
22
votes
4 answers
How to hint to GCC that a line should be unreachable at compile time?
It's common for compilers to provide a switch to warn when code is unreachable. I've also seen macros for some libraries, that provide assertions for unreachable code.
Is there a hint, such as through a pragma, or builtin that I can pass to GCC (or…

Matt Joiner
- 112,946
- 110
- 377
- 526
21
votes
8 answers
What should I do when I am forced to write unreachable code?
I have this simple piece of code:
public static int GetInt(int number)
{
int[] ints = new int[]{ 3, 7, 9, int.MaxValue };
foreach (int i in ints)
if (number <= i)
return i;
return int.MaxValue; //this should be…

Bosak
- 2,103
- 4
- 24
- 43
16
votes
3 answers
What's compiler thinking about the switch-statement?
Inspired from a -5 question again!
Is empty case of switch in C# combined with the next non-empty one?
I read [this comment] of @Quartermeister and been astonished!
So why this compiles
switch(1) {
case 2:
}
but this doesn't.
int…

Ken Kin
- 4,503
- 3
- 38
- 76
11
votes
4 answers
Java: How to @SuppressWarnings unreachable code?
Sometimes when you are debugging, you have unreachable code fragment. Is there anyway to suppress the warning?

Mohammad Moghimi
- 4,636
- 14
- 50
- 76
10
votes
3 answers
Unreachable statement: while true vs if true
How should I understand this Java compiler behaviour?
while (true) return;
System.out.println("I love Java");
// Err: unreachable statement
if (true) return;
System.out.println("I hate Java");
// OK.
Thanks.
EDIT:
I find out the point after a few…

marek094
- 424
- 2
- 11
10
votes
1 answer
Scala compiler says unreachable code, why?
I'm new to Scala... Here's the code:
def ack2(m: BigInt, n: BigInt): BigInt = {
val z = BigInt(0)
(m,n) match {
case (z,_) => n+1
case (_,z) => ack2(m-1,1) // Compiler says unreachable code on the paren of ack2(
…

mentics
- 6,852
- 5
- 39
- 93
10
votes
6 answers
Unreachable statement compile error in Java
class For1
{
public static void main(String args[])
{
int a = 0;
for(;;)
{
break;
System.out.println(a); //Line 1
++a;//Line 2
}
}
}
I know that Line 1/Line 2 will never be executed.
But still I don't…

UnderDog
- 3,173
- 10
- 32
- 49