有什么方法可以强制编译器(注解或其他方式)实现 java 函数永远不会返回(即总是抛出),以便随后它不会错误地将其用法作为返回非 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();
}
谢谢你。