San*_*Roy 0 java exception-handling arithmeticexception
我无法理解这个程序.我希望它输出"Hello World",但它只打印"World".我认为首先try块会执行,打印"Hello"和"",然后在遇到a时1/0,它会抛出一个ArithmeticException.该例外将被catch阻止,然后将打印"世界".
该计划如下.
import java.util.*;
class exception{
public static void main(String args[])
{
try
{
System.out.println("Hello"+" "+1/0);
}
catch(ArithmeticException e)
{
System.out.println("World");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在调用println函数之前抛出异常.必须在函数调用之前计算参数值.
为了使您的程序达到预期的结果,您可以try按如下方式编辑块中的代码:
try
{
// this will work and execute before evaluating 1/0
System.out.print("Hello ");
// this will throw the exception
System.out.print(1/0);
}
catch(ArithmeticException e)
{
System.out.println("World");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46 次 |
| 最近记录: |