【发布时间】:2017-03-26 08:02:44
【问题描述】:
package testing;
public class ExceptionHandling {
public static void main(String[] args){
try{
int a=10;
int b=0;
int c=a/b;
ExceptionHandling exp = null;
System.out.println(exp);
throw new NullPointerException();
}catch(ArithmeticException e){
System.out.println("arithmetic issue");
throw new ArithmeticException();
}
catch(NullPointerException e){
System.out.println("nullpointer");
}
finally{
System.out.println("exception");
//throw new ArithmeticException();
}
}
}
在控制台中我得到了这个:
arithmetic issue
exception
Exception in thread "main" java.lang.ArithmeticException
at testing.ExceptionHandling.main(ExceptionHandling.java:15)
但是为什么会先打印 finally 块语句,然后再打印 catch 块语句呢?它应该先打印 catch 块语句,然后打印 finally 块语句。
【问题讨论】:
-
您的输出首先打印
catch块,然后是finally。为什么你认为它是打印的。 -
检查这个答案。 link。此外,您不需要在 try 块本身中引发异常。
标签: java exception-handling finally