捕获数字formatexception具有不同的行为?

发布于 2025-01-30 17:46:59 字数 1531 浏览 3 评论 0原文

我正在编写一个代码来显示尝试/捕获的示例,我注意到相同的catch numberFormateXception是触发或未取决于我使用的位置。这是代码:

public class Main {
public static void main(String[] args) {

    System.out.println("3/0 => Result: " + divide(3,0));  // returns 3/0 => Result: null
    System.out.println("6/2 => Result: " + divide(6,2));  // returns 3/0 => Result: 3
//        System.out.println("6/home => Result: " + divide(Integer.parseInt("home"),1));

    try {
        System.out.println("6/home => Result: " + divide(Integer.parseInt("home"),1));
    } catch (NumberFormatException e) {
        System.out.println("Error type: NumberFormatException (MAIN METHOD)");
    }
}

static Integer divide(int n1, int n2) {   // we used Integer (wrapper class) to be able to return null

    int result = 0;

    try {
        result = n1 / n2;
    } catch (ArithmeticException e) {
        System.out.println("Error type: ArithmeticException");
        return null;
    } catch (NumberFormatException e) {
        System.out.println("Error type: NumberFormatException");
        return null;
    }
    return result;
}
}

代码返回:

Error type: ArithmeticException
3/0 => Result: null
6/2 => Result: 3
Error type: NumberFormatException (MAIN METHOD)

但是,如果我启用第三行:

System.out.println("6/home => Result: " + divide(Integer.parseInt("home"),1));

并禁用MAIN方法内的try/Catch,divide方法中的nubmefformatexception不会触发,并且它将程序崩溃。

谁能解释为什么?我正在使用相同的异常类型,为什么它在主方法中起作用,但在鸿沟方法中不起作用?

I was writing a code to show examples of try/catch and I notice that the same catch NumberFormatException was triggered or not depending on where I use it. This is the code:

public class Main {
public static void main(String[] args) {

    System.out.println("3/0 => Result: " + divide(3,0));  // returns 3/0 => Result: null
    System.out.println("6/2 => Result: " + divide(6,2));  // returns 3/0 => Result: 3
//        System.out.println("6/home => Result: " + divide(Integer.parseInt("home"),1));

    try {
        System.out.println("6/home => Result: " + divide(Integer.parseInt("home"),1));
    } catch (NumberFormatException e) {
        System.out.println("Error type: NumberFormatException (MAIN METHOD)");
    }
}

static Integer divide(int n1, int n2) {   // we used Integer (wrapper class) to be able to return null

    int result = 0;

    try {
        result = n1 / n2;
    } catch (ArithmeticException e) {
        System.out.println("Error type: ArithmeticException");
        return null;
    } catch (NumberFormatException e) {
        System.out.println("Error type: NumberFormatException");
        return null;
    }
    return result;
}
}

The code returns:

Error type: ArithmeticException
3/0 => Result: null
6/2 => Result: 3
Error type: NumberFormatException (MAIN METHOD)

But if I enable the 3rd line:

System.out.println("6/home => Result: " + divide(Integer.parseInt("home"),1));

And disable the try/catch inside main method the NumberFormatException inside the divide method does not trigger and it crash the program.

Can anyone explain why? I am using the same exception type, why it works inside the main method but it does not work inside the divide method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2025-02-06 17:46:59

捕获异常取决于相关性尝试{}块中发生的异常。通过在主方法中禁用试用 /捕获,偶然的数字格式异常不再在任何尝试{}块中,因此会“崩溃”您的程序。在这种情况下,鸿沟方法内的尝试 /捕获甚至从未达到过,因为在线程执行代码的integer.parseint(“ home”)'时发生了例外,该'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''是“ home'')''''thit of the block / of the block / of the bloce / of the Main方法。

Catching exceptions is depending on the exception happening inside the corresponsing try{} block. By disabling the try / catch inside the main method the occuring number format exception is not in any try{} block anymore and therefore "crashes" your program. The try / catch inside the divide method is in that case never even reached, as the exception happens while the thread is executing the code 'Integer.parseInt("home")' which is outisde of that block / inside your main method.

三五鸿雁 2025-02-06 17:46:59

这是因为integer.parseint < / code>方法是从主方法而不是在try / catch块中调用的。查看应打印出来的堆栈跟踪。

Exception in thread "main" java.lang.NumberFormatException: For input string: "home"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Main.main(Main.java:6)

最后一行(main.main(main.java:6))为您提供了一个明确的提示,从代码中抛出例外情况。

It's because the Integer.parseInt method is called from the main method and not inside try / catch block. Look at the stack trace that should be printed out.

Exception in thread "main" java.lang.NumberFormatException: For input string: "home"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at Main.main(Main.java:6)

The last line (Main.main(Main.java:6)) is giving you a clear hint from where the exception was thrown out in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文