有什么方法可以迫使编译器(注释或其他方法)实现一个Java函数永不返回(即始终抛出),以便随后它不会将其用法作为其他函数中的最后一个语句返回non void的错误。
这是一个简化/虚构的示例:
int add( int x, int y ) {
throwNotImplemented(); // compiler error here: no return value.
}
// How can I annotate (or change) this function, so compiling add will not yield
// an error since this function always throws?
void throwNotImplemented() {
... some stuff here (generally logging, sometimes recovery, etc)
throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
谢谢。