为什么我不能将异常处理逻辑放在一个单独的方法中而没有返回值?

huangapple go评论76阅读模式
英文:

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.");
}
}

huangapple
  • 本文由 发表于 2023年3月23日 09:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定