Re: Warning: Unreachable code detected
Peter Ritchie <[email protected]> Mon, 12 Nov 2007 12:19:53 -0500
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
"throw" is one in a class of statements considered "jump statements".
This means, anything that occurs after a jump statement is considered
unreachable. This will occur for "break", "return", "goto", and "continue"
as well. In C# you'll get warning CS0162 for each of the cases in the
following:
while (true)
{
switch ((int)source)
{
case 1:
return;
break;
case 2:
goto label;
break;
case 3:
continue;
break;
case 4:
throw new Exception();
break;
case 5:
break;
break;
}
}
label:
return;
There's nothing you can do short of re-arranging the code. But, I would
consider it the same as the other jump statements (including break) as
being one of 5 statements that can appear at the end of a case block.
I.e. I wouldn't re-arrange the code and risk making it confusing and
harder to maintain just because you want to always end your case blocks
with a "break;".