捕获数字formatexception具有不同的行为?
我正在编写一个代码来显示尝试/捕获的示例,我注意到相同的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
捕获异常取决于相关性尝试{}块中发生的异常。通过在主方法中禁用试用 /捕获,偶然的数字格式异常不再在任何尝试{}块中,因此会“崩溃”您的程序。在这种情况下,鸿沟方法内的尝试 /捕获甚至从未达到过,因为在线程执行代码的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.
这是因为
integer.parseint < / code>方法是从主方法而不是在try / catch块中调用的。查看应打印出来的堆栈跟踪。
最后一行(
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.The last line (
Main.main(Main.java:6)
) is giving you a clear hint from where the exception was thrown out in your code.