Java expects return value when RuntimeException is thrown

gkumar7 :

Why does this not compile (tried with java 8 and java 10)? It yields a missing return statement error.

 public class CompilerIssue {
   public boolean run() {
     throwIAE();
     // Missing return statement
   }

   public void throwIAE() {
     throw new IllegalStateException("error");
   }
 }
DwB :

The java compiler does not know that the throwIAE will always throw an exception so it assumes that you will eventually reach the end of the run method and, when that happens, a return value is required.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=84270&siteId=1