为什么 Java IDE 的缺省实现模板不会抛出断言错误?



当你在ide中生成/覆盖新方法,生成try-catch构造等时,它们中的大多数(如Eclipse, Intellij)默认会有相当可疑的行为,如返回null,打印stacktrace和continue,调用super method等。例如,如果你在'Thread.sleep(x)'上自动完成,IDE会给你:

try {
    Thread.sleep(x);
}catch(InterruptedException e){
    e.printStackTrace();
}

在我看来,在这种情况下更明智的做法是throw new AssertionError(e),在其他情况下是throw new AssertionError("not implemented")

关于为什么它们不是一个"更好"的默认值的简短答案是,对于"更好"的默认值是什么,它可能完全是主观的:)

下面是如何在IntelliJ中配置:

Settings -> [IDE Settings] File and Code Templates -> [tab: Code] Catch Statement body

enter: throw new AssertionError("not implemented");

最新更新