title: Missing return statement in a non-void method compiles
body: <p>I encountered a situation where a <strong>non-void method</strong> is missing a <strong>return</strong> statement and the code still compiles.
  I know that the statements after the while loop are <strong>unreachable</strong> (dead code) and would never be executed. But why doesn't the compiler even warn about returning something? Or why would a language allow us to have a non-void method having an infinite loop and not returning anything?</p>
  
  <pre><code>public int doNotReturnAnything() {
      while(true) {
          //do something
      }
      //no return statement
  }
  </code></pre>
  
  <p>If I add a break statement (even a conditional one) in the while loop, the compiler complains of the infamous errors: <code>Method does not return a value</code> in Eclipse and <code>Not all code paths return a value</code> in Visual Studio.</p>
  
  <pre><code>public int doNotReturnAnything() {
      while(true) {
          if(mustReturn) break;
          //do something
      }
      //no return statement
  }
  </code></pre>
  
  <p>This is true of both Java and C#.</p>