tags:

views:

81

answers:

3

Why does the MS C# compiler complain that "not all code paths return a value" in the following scenario?

public int Foo(bool flag)
{
    if(flag)
    {
        return 1;
    }
    else
    {
        ThrowException(); // this method always throws an exception

        // return -1; // why do I need to add this code that will never be called?
    }
}

Thanks!

+5  A: 

It can't guess that ThrowException() is a method that always throws exceptions. For that you'd need static code analysis.

Static code analysis is available in VS 2010, but only for the more expensive versions of VS, I believe.

devoured elysium
Absolutely right. But to clarify the OPs point, if you change the method ThrowException() to return an Exception and change the code to: throw ThrowException(), It would behave like the OP wanted.
Kirk Woll
Well, VS 2008 does have static code analysis, otherwise it wouldn't raise this error.
Christopher
@Kirk: I don't get the point of doing so. I don't even get the point of having a method always throwing an Exception. I guess this is more of a theoretical question, though.
devoured elysium
@Christopher: When we talk of static code analysis we mean a more elaborated kind of analysis. Although it is obvious for a human that your code will always either throw something or return something, it currently isn't for the compiler.
devoured elysium
Can anyone confirm that the premium version of VS 2010 handles this correctly?
Christopher
I've tried running it. I think it can't handle it, though.
devoured elysium
@devoured elysium: Sometimes it can be useful to have a delegate throw an exception (e.g. a file-parsing method performs a callback if an error occurs; that callback may log the problem and/or throw an exception or do something else). Generally, though, I think it will be more useful to have the exception thrown nearer the source of the problem.
supercat
FYI- in my case, the method is actually called ThrowWrappedException(), which is a helper method that adds more detail to the exception.
Christopher
+1  A: 

The else branch does not have a return statement. That means that Foo does not return a value when entering the else branch.

Why don't you do:

public int Foo(bool flag)
{
    if (!flag) {
        ThrowException();
    }

    return 1;
}

BTW I always feel that validation should be done first.

thelost
Thanks--that's a good idea
Christopher
Yes, I also believe this code is way more understandable. Plus it has less indentation.
devoured elysium
A: 

The error message says it all: Not all code path return a value. I reckon the purpose of ThrowException() is to throw an exception. If it does so, I can see how the error message would seem strange. However, consider the consequence of accepting this a valid code. If the implementation of ThrowException() was changed at a later point to no longer throw an exception, the code above would suddenly fail, and that would probably come as a big surprise. The compiler is picking the safe road here.

Brian Rasmussen