Compiler bug: Not All code paths return a value

  • Thread starter Thread starter Niall
  • Start date Start date
N

Niall

I was compiling the function below and received this error. Putting a return
statement after the loop solves the problem I believe this is a compiler
bug, as this code will always leave the function within the loop.

protected DateTime GetDate()
{
for (int i = 0; i < 3; i++)
{
try
{
return DateTime.Now;
}
catch
{
throw;
}
}
}

Niall
 
This is not a bug - although for() is a well-known construct it still
requires run-time evaluation to test the entry to the loop. On the other
hand this:

static double GetValue()
{
for(;true;)
{
return 10d;
}
}

is compile-time deterministic and does compile.
Richard
 
Ah, of course. I was not considering the case (for the compiler) where the
loop is not entered, I was thinking the compiler thought it could exit the
loop.

So this post ends up in the same basket as so many other bug reports -
Denied :P

Niall
 
Back
Top