英文:
Why can't I put exception handling logic in separate method without a return?
问题
Here's the translated code:
我有以下代码:
public class Scratch {
public Scratch() {
doStuff();
}
public String doStuff() {
try {
int i = Integer.parseInt(null);
return "Integer: " + i;
} catch (NumberFormatException e) {
if (e.getMessage().equals("some message")) {
throw new NumberFormatException("step 1");
} else {
throw new NumberFormatException("step 2");
}
}
}
public static void main(String[] args) {
new Scratch();
}
}
我想将异常处理逻辑放在自己的方法中,像这样:
public class Scratch {
public Scratch() {
doStuff();
}
public String doStuff() {
try {
int i = Integer.parseInt(null);
return "Integer: " + i;
} catch (NumberFormatException e) {
handleException(e);
}
}
private void handleException(NumberFormatException e) {
if (e.getMessage().equals("some message")) {
throw new NumberFormatException("step 1");
} else {
throw new NumberFormatException("step 2");
}
}
public static void main(String[] args) {
new Scratch();
}
}
但是我得到一个编译器错误,说`doStuff`方法缺少返回语句。
我不明白为什么这些看似相等的实现不同。
英文:
I have the following code:
public class Scratch {
public Scratch() {
doStuff();
}
public String doStuff() {
try {
int i = Integer.parseInt(null);
return "Integer: " + i;
} catch (NumberFormatException e) {
if (e.getMessage().equals("some message")) {
throw new NumberFormatException("step 1");
} else {
throw new NumberFormatException("step 2");
}
}
}
public static void main(String[] args) {
new Scratch();
}
}
and I want to put the exception handling logic in its own method like this:
public class Scratch {
public Scratch() {
doStuff();
}
public String doStuff() {
try {
int i = Integer.parseInt(null);
return "Integer: " + i;
} catch (NumberFormatException e) {
handleException(e);
}
}
private void handleException(NumberFormatException e) {
if (e.getMessage().equals("some message")) {
throw new NumberFormatException("step 1");
} else {
throw new NumberFormatException("step 2");
}
}
public static void main(String[] args) {
new Scratch();
}
}
But I am getting a compiler error saying that the doStuff
method is missing a return statement.
I don't understand why these seemingly equivalent implementations differ.
答案1
得分: 3
我认为这是因为编译器无法确定 handleException()
总是会抛出异常。如果它以正常方式返回,那么代码将继续执行,并在方法结束时没有返回语句。
在这种情况下,惯例是只抛出一个带有适当消息的 AssertionError
。
public String doStuff() {
try {
int i = Integer.parseInt( null );
return "Integer: " + i;
} catch( NumberFormatException e ) {
handleException( e );
throw new AssertionError( "应该永远不会到这里。");
}
}
英文:
I think it's because the compiler can't figure that handleException()
always throws an exception. If it ever returned normally then the code would just keep executing, and reach the end of the method with no return statement.
In these situations it's convention to just throw an AssertionError
with an appropriate message.
public String doStuff() {
try {
int i = Integer.parseInt( null );
return "Integer: " + i;
} catch( NumberFormatException e ) {
handleException( e );
throw new AssertionError( "Should never get here.");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论