Java expects return value when RuntimeException is thrown

  • A+
Category:Languages

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");    }  } 

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: