Why is the code not available here?

I am writing an aC # application and want to display error messages on the console or in the message box (depending on the type of application: enum AppTypeChoice {Console, Windows}), and also control how the application continues to work or not (bool StopOnError).

I came up with this method that will check all the criteria, but I get a warning “unreachable code”. I do not understand why!

Here is the whole method (grab some code for fans!)


    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, if I have your attention to get the type of application, I just use the constant at the top of the file (e.g. AppTypeChoice.Console in the console application, etc.) - is there a better way to do this (I mean figure out the code if it is a DOS or Windows application)?

, , ... ( , , ?! )

+5
5

DataDefs.StopOnError - , false.

false ( , ) .

:

if (false == true)
    Environment.Exit(0); // UNREACHABLE CODE HERE

.

DataDefs.StopOnError readonly const.

, , DataDefs.StopOnError .

+10

DataDefs.StopOnError , "if" . , .

+2

...

DebugLevelChoice level = DataDefs.DebugLevel;

        // Decide how much info we should give out here...
        if (level != DebugLevelChoice.None)

, level DebugLevel, None. , , , , .

+1

, , , . , , .

, , , appType (AppTypeChoice.Console), :

// Give some info....
if (appType == AppTypeChoice.Windows)
    MessageBox.Show(message, defaultMsgBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
else
    Console.WriteLine(dosBoxOutput);
0

As for the message box, it is not recommended to use the message box to transfer data from the command line. When a message box appears, this will prevent the command line program from executing until the user interacts with it. This can lead to problems when some other program calls your program and no one wants to click OK. It is better to use the console to display errors; other developers will thank you for not breaking the message box.

0
source

All Articles