Java 實例 - 鏈試異常
以下實例演示了使用多個 catch 來處理鏈試異常:
/* author by w3cschool.cc Main.java */ public class Main{ public static void main (String args[])throws Exception { int n=20,result=0; try{ result=n/0; System.out.println("結(jié)果為"+result); } catch(ArithmeticException ex){ System.out.println("發(fā)算術(shù)異常: "+ex); try { throw new NumberFormatException(); } catch(NumberFormatException ex1) { System.out.println("手動拋出鏈試異常 : "+ex1); } } } }
以上代碼運行輸出結(jié)果為:
發(fā)算術(shù)異常: java.lang.ArithmeticException: / by zero 手動拋出鏈試異常 : java.lang.NumberFormatException
更多建議: