Im Allgemeinen propagiert der Java-Compiler nicht die Information, dass eine Methode "immer" eine Exception auslöst und daher nicht erkennt, dass alle Codepfade vollständig sind .Ist es möglich, dem Compiler mitzuteilen, dass eine Methode immer eine Exception auslöst
(Dies ist aufgrund der Tatsache, dass Java-Compiler jede Klasse unabhängig kompiliert).
Es ist ein Problem, wenn Sie so etwas schreiben möchten.
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");
}
}
(dh eine Art "Assertion Helper", die Kontextinformationen erfasst).
Da der Compiler beschweren wird, dass myMethod nicht immer ein MyObject zurückgibt.
Nach meinem Wissen gibt es keine spezifische Anmerkung, die angibt, dass eine Methode immer ausgelöst wird.
Eine Lösung in einem anderen Beitrag [hier] (http://stackoverflow.com/a/11249665/1402846). – Pang