Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java expects return value when RuntimeException is thrown

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");
   }
 }
like image 267
gkumar7 Avatar asked Nov 27 '22 06:11

gkumar7


1 Answers

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.

like image 98
DwB Avatar answered Dec 28 '22 08:12

DwB