Why is there unreachable code here?
Posted
by Richard
on Stack Overflow
See other posts from Stack Overflow
or by Richard
Published on 2010-04-25T21:38:41Z
Indexed on
2010/04/25
21:43 UTC
Read the original article
Hit count: 351
c#
I am writing a c# app and want to output error messages to either the console or a messagebox (Depending on the app type: enum AppTypeChoice { Console, Windows } ), and also control wether the app keeps running or not ( bool StopOnError ).
I came up with this method that will check all the criteria, but I'm getting an "unreachable code detected" warning. I can't see why!
Here is the whole method (Brace yourselves for some hobbyist code!)
public void OutputError(string message)
{
string standardMessage = "Something went WRONG!. [ But I'm not telling you what! ]";
string defaultMsgBoxTitle = "Aaaaarrrggggggggggg!!!!!";
string dosBoxOutput = "\n\n*** " + defaultMsgBoxTitle + " *** \n\n Message was: '" + message + "'\n\n";
AppTypeChoice appType = DataDefs.AppType;
DebugLevelChoice level = DataDefs.DebugLevel;
// Decide how much info we should give out here...
if (level != DebugLevelChoice.None)
{
// Give some info....
if (appType == AppTypeChoice.Windows)
MessageBox.Show(message, defaultMsgBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
else
Console.WriteLine(dosBoxOutput);
}
else
{
// Be very secretive...
if (appType == AppTypeChoice.Windows)
MessageBox.Show(standardMessage, defaultMsgBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
else
Console.WriteLine(standardMessage);
}
// Decide if app falls over or not..
if (DataDefs.StopOnError == true)
Environment.Exit(0); // UNREACHABLE CODE HERE
}
Also, while I have your attention, to get the app type, I'm just using a constant at the top of the file (ie. AppTypeChoice.Console in a Console app etc) - is there a better way of doing this (i mean finding out in code if it is a DOS or Windows app)?
Also, I noticed that I can use a messagebox with a fully-qualified path in a Console app...How bad is is to do that ( I mean, will I get tarred and feathered when other developers see it?!)
Thanks for your help
© Stack Overflow or respective owner