When a runtimeException is thrown, Java expects the return value

Why doesn't this compile (try Java 8 and Java 10)? It generates a missing return statement error

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

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

Solution

Throwiae, which the java compiler doesn't know, will always throw an exception, so it assumes that you will eventually reach the end run method and. When this happens, you need to return a value

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>