3

I have a method in a C# program that essentially looks like this:

public bool Connect()
{
    try
    {
        // try to connect
        return true;
    }
    catch(MySqlException e)
    {
        throw new CustomException(e.Message);
        return false;
    }
}

The issue is about the order of these specific lines. The compiler complains about this and indicates the return statement as unreachable code:

throw new CustomException(e.Message);
return false;

But it does not complain about this:

return false;
throw new CustomException(e.Message);

Why does the first one not work, but the second one does? Doesn't a method exit as soon as it hits a return statement?

user2864740
  • 60,010
  • 15
  • 145
  • 220
David Mordigal
  • 399
  • 3
  • 19
  • 3
    They both "work". And in both cases, the second line is unreachable. So it seems what you really want to know is, why does the throw after the return not generate a compile warning too? – hatchet - done with SOverflow Apr 11 '15 at 22:24
  • 1
    C# uses an imperfect heuristic to detect unreachable code, because you cannot deterministically detect all unreachable code (that would require solving the [halting problem](https://en.wikipedia.org/wiki/Halting_problem), which is impossible). They simply haven’t built in a rule to detect your second statement, even though it’s definitely unreachable. – Konrad Rudolph Apr 11 '15 at 22:27
  • 1
    To any future people wanting to (attempt to) answer this question. 1. Please read the *whole* question before attempting to answer. 2. The question is really this: Why does code that first throws then returns produce a compiler warning, whereas if you first return, then throw, doesn't? – Lasse V. Karlsen Apr 11 '15 at 22:27
  • The same compiler warning is reproduce (and not reproduced, respectively) when removing the `try/catch`. – user2864740 Apr 11 '15 at 22:28
  • @KonradRudolph Which seems silly as `return` is 'definitely terminating'; Java has the algorithm rules well defined, and I believe C# does although I am much less familiar with its spec. – user2864740 Apr 11 '15 at 22:29
  • 2
    I agree that this question is exactly that, a duplicate of the linked question, however the "answer" there does not in any way answer the question. It basically says "that's just how it is". – Lasse V. Karlsen Apr 11 '15 at 22:30
  • @LasseV.Karlsen, still seems like it should be closed as a dup, but if you think I should undo my vote, I'll oblige. – Kirk Woll Apr 11 '15 at 22:31
  • 2
    An even better explanation is here (from Eric Lippert himself): http://stackoverflow.com/questions/6371564/why-does-throwing-2-exceptions-in-a-row-not-generate-an-unreachable-code-warning – hatchet - done with SOverflow Apr 11 '15 at 22:32
  • 1
    I reopened it. A question specifically asking *why* is in no way a duplicate of a question saying "that's just how it is". Just as you, if someone else votes to close it as a duplicate, I'll leave it alone, but in my opinion the linked question does in no way answer it. To be honest, I fail to see how the answer on that question answers that question too. A question about "why" is never answered with "because". – Lasse V. Karlsen Apr 11 '15 at 22:33
  • And yes, I agree, @KirkWoll probably has the best duplicate so I'll mark this as a duplicate of that instead. – Lasse V. Karlsen Apr 11 '15 at 22:33
  • @LasseV.Karlsen, think you meant hatchet, but I agree with everything you said. :) – Kirk Woll Apr 11 '15 at 22:34
  • 3
    Uhm, yes, whiskey, evening, tired, you know how it goes. – Lasse V. Karlsen Apr 11 '15 at 22:34
  • Well, it looks like its been in the bug database for years and they still haven't fixed it. Probably very low on the priority list. – Jesse Good Apr 11 '15 at 22:36
  • I wonder what ReSharper thinks of that code... And what about code analysis. – Lasse V. Karlsen Apr 11 '15 at 22:38
  • 1
    OK, ReSharper correctly "flags" both unreachable lines as unreachable by coloring them grey. Code analysis is oblivious to this problem. Pop quiz, did you know you can integrate ReSharper code analysis into your build process using the [ReSharper command line tool](https://www.jetbrains.com/resharper/features/command-line.html) ? – Lasse V. Karlsen Apr 11 '15 at 22:42

0 Answers0