Can't understand this "Unreachable code detected" error
I have this snippet of code that is giving me problems.
The last break statements gives an error of "unreachable code detected"
this has caused a logical flaw in my program.
can someone identify the problem please?
thanks!
case 127232:
case1 = splited[0];
changeRow[inServer] = "Audit Privileges and Logs:\n";
changeRow[inServer + " Exception"] = "No Exception";
writeToFile(Directory.GetCurrentDirectory() + @"\" + inServer + "127232.txt", case1);
if (case1.Contains("audit level;failure") || case1.Contains("audit level;all"))
{
Console.WriteLine("success!");
changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) Audit options found .";
changeRow[inServer] = changeRow[inServer] + "string pattern audit level;failure or auditlevel;all found";
break;
}
else
{
//Audit Privileges should have been in enabled for sys and system accounts or the security administrator.
开发者_JAVA百科 changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) No audit options found.";
changeRow[inServer] = changeRow[inServer] + "Correct audit options not configured!";
changeRow[inServer + " Exception"] = "Exception";
break;
}
break;
Both the then
and the else
part of your if
clause contain a break
. So obviously the code after the if
will never be executed.
You have a break
in both your if
and your else
clause, meaning that the break
at the bottom will never be reached.
It doesn't really matter in this particular case but it seems the compiler is not "smart" enough to figure that out. That's not necessarily a bad attitude for the compiler to take since this could be considered sloppy coding practice.
Just get rid of both break
statements within the if
and else
clauses or, alternatively, refactor to:
case 127232:
case1 = splited[0];
changeRow[inServer] = "Audit Privileges and Logs:\n";
changeRow[inServer + " Exception"] = "No Exception";
writeToFile(Directory.GetCurrentDirectory() + @"\" + inServer + "127232.txt", case1);
if (case1.Contains("audit level;failure") || case1.Contains("audit level;all"))
{
Console.WriteLine("success!");
changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) Audit options found .";
changeRow[inServer] = changeRow[inServer] + "string pattern audit level;failure or auditlevel;all found";
break;
}
//Audit Privileges should have been in enabled for sys and system accounts or the security administrator.
changeRow[inServer + "Observations"] = changeRow[inServer + "Observations"] + "1) No audit options found.";
changeRow[inServer] = changeRow[inServer] + "Correct audit options not configured!";
changeRow[inServer + " Exception"] = "Exception";
break;
What you have in your original is not really that much different to something like:
if (someCondition) {
doSomething();
return;
} else {
doSomethingElse();
}
which I always think is cleaner as:
if (someCondition) {
doSomething();
return;
}
doSomethingElse();
I don't like being tabbed to death by code unless it's absolutely necessary :-)
Because your code will go into either the if
or the else
and they both contain a break
, which will take you outside of your switch
statement, the last break
statement is unnecessary.
There are breaks in if Condition and Else condition. How can it reack break after else condition?
精彩评论