Is it possible to tell the compiler that a method always throws an Exception
A simple workaround is to let your fatalISE
method not throw the exception, but only create it:
public class ErrorContext {
public IllegalStateException fatalISE(String message) {
String context = "gather lots of information about the context of the error";
return new IllegalStateException(context +": " + message);
}
}
public class A {
public MyObject myMethod() {
if (allIsGood()) {
return new MyObject();
}
throw ErrorContext.fatalISE("all is not good");
}
}
This way the compiler will know not to complain about a missing return
. And forgetting to use the throw
is unlikely, exactly because the compiler will usually complain.
A trick that I use is to replace
public void fatalISE(String message) {
String context = "gather lots of information about the context of the error";
throw new IllegalStateException(context +": " + message);
}
with
public <T> T fatalISE(String message) {
String context = "gather lots of information about the context of the error";
throw new IllegalStateException(context +": " + message);
}
Then, in myMethod, use:
public MyObject myMethod() {
if (allIsGood()) {
return new MyObject();
}
return ErrorContext.fatalISE("all is not good");
}
It will work whatever the return type of myMethod, including primitive types. You can still use fatalISE
in a void method, by just not using the return
keyword.
How about reversing the if condition?
public MyObject myMethod() {
if (!allIsGood()) {
ErrorContext.fatalISE("all is not good");
}
return new MyObject();
}
Good luck!