我试图在try-catch块中添加我的一些代码,但它在编译时失败,我没有得到错误.
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
Run Code Online (Sandbox Code Playgroud)
请有人帮忙吗.
使用 :
try
{
int x = 0;
int y = 5 / x;
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
catch (Exception e)
{
System.out.println("Exception");
}
System.out.println("finished");
Run Code Online (Sandbox Code Playgroud)
在处理异常时,更广泛的异常(超类异常,在你的情况下,异常是ArthmeticException的超类)必须在捕获子类异常后捕获.否则,异常将被更宽/父异常catch块捕获,后面的代码将无法访问.所以它不会编译.