Generally speaking, the Java compiler does not propagate the information that a method "always" throw an Exception, and therefore, does not detect that all code paths are complete.
(This is due to the fact that Java compiler compiles each class independently).
It's a problem when you want to write something like that.
public class ErrorContext?{
public void fatalISE(String message)?{
String context = "gather lots of information about the context of the error";
throw new IllegalStateException(context +": " + message);
}
}
public class A {
public MyObject myMethod()?{
if (allIsGood())?{
return new MyObject();
}
ErrorContext.fatalISE("all is not good");
}
}
(ie, a kind of "assertion helper" that gathers context information).
Because the compiler will complain that myMethod does not always return a MyObject.
To my knowledge, there is no specific annotation to indicate that a method always throws.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…