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)
但爲什麼它會打印終於第一塊語句,然後搭上塊語句?它應該先打印catch塊語句,然後再打印塊語句。
你的輸出先打印'catch'塊,然後打印'finally'。爲什麼你認爲這是打印否則。 – Codebender
查看此答案。 [鏈接](http://stackoverflow.com/questions/3109353/what-comes-first-finally-or-catch-block)。另外,你不需要在try塊本身中拋出異常。 – budthapa