Re: Warning: Unreachable code detected
Mike Andrews <[email protected]> Mon, 12 Nov 2007 11:42:18 -0600
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
The compiler warning is given in that the break statement is unreachable code irrespective of the default case. On Nov 12, 2007 11:20 AM, Bhatnagar, Amit <[email protected]> wrote: > try: > > case 3: > throwException = true; > break; > > (you should always have a default case) > default: > assert(false); //unhandled case > break; > > if (throwException) > { > throw new ApplicationException("No such logon session.") > } > > -----Original Message----- > From: Discussion forum for developers using Windows Forms to build apps > and controls [mailto:[email protected]] On Behalf Of > R. Rogers > Sent: Monday, November 12, 2007 10:15 AM > To: [email protected] > Subject: [DOTNET-WINFORMS] Warning: Unreachable code detected > > Hi, > > I like coding defensively, and correct me if I'm wrong, but I think the > following code demonstrates this to a very small degree: > > switch (code) > { > case 1: > result = DialogResult.OK; > break; > > case 2: > result = DialogResult.Cancel; > break; > > case 3: > throw new ApplicationException("No such logon > session."); > break; > > ... > } > > The trouble is that if I break after throwing an exception, the compiler > gives me an "unreachable code detected" message, and I don't like any > warnings. > > Would you have any advice to share on this? I don't really want to get > rid of the break statement. > > Best regards, > > Richard Rogers > http://www.RichardRogers.ca/ <http://www.richardrogers.ca/> > > The information contained in this e-mail message is PRIVATE. It may > contain confidential information and may be legally privileged. It is > intended for the exclusive use of the addressee(s). If you are not the > intended recipient, you are hereby notified that any dissemination, > distribution or reproduction of this communication is strictly prohibited. > If the intended recipient(s) cannot be reached or if a transmission problem > has occurred, please notify the sender immediately by return e-mail and > destroy all copies of this message. > Thank you. >