Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there unreachable code here?

Tags:

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

like image 866
Richard Avatar asked Apr 25 '10 21:04

Richard


People also ask

Why does it say my code is unreachable?

The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

Why am I getting unreachable code in Java?

Unreachable code error occurs when the code can't be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.

What is unreachable code Vscode?

Unreachable code, a part of the source code that will never be executed due to inappropriate exit points/control flow. The other kind of unreachable code is referred as dead code, although dead code might get executed but has no effect on the functionality of the system.


3 Answers

DataDefs.StopOnError is a compile-time constant equal to false.

Therefore, the compiler replaces it with false (Or whatever you set it to) near the begining of the compiliation process.

Therefore, your code compiles to:

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

This is obviously unreachable.

The simplest solution is to make the DataDefs.StopOnError field readonly instead of const.

The compiler will only give this warning if all of the values involved are compile-time constants or literals, so using any other type of field for DataDefs.StopOnError will stop the warning.

like image 163
SLaks Avatar answered Sep 28 '22 07:09

SLaks


If the value of DataDefs.StopOnError is false, then the body of the "if" will be unreachable. Check to see if that's the default.

like image 38
duffymo Avatar answered Sep 28 '22 08:09

duffymo


Look specifically at this code...

DebugLevelChoice level = DataDefs.DebugLevel;

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

The code above will be unreachable because you set level to always be DebugLevel so it'll never be None. It'd help us further if you could tell us more about what the error says, for example, what lines its on or which code is unreachable.

like image 36
Chris Avatar answered Sep 28 '22 06:09

Chris