java入門---異常處理例項之使用 catch 處理異常
阿新 • • 發佈:2019-02-12
以下例項演示了使用 catch 來處理異常的方法:
public class Main {
public static void main (String args[]) {
int array[]={20,20,40};
int num1=15,num2=10;
int result=10;
try{
result = num1/num2;
System.out.println("結果為 " +result);
for(int i =5;i >=0; i--) {
System.out.println ("陣列的元素值為 " +array[i]);
}
}
catch (Exception e) {
System.out.println("觸發異常 : "+e);
}
}
}
以上程式碼執行輸出結果為:
結果為1觸發異常: java.lang.ArrayIndexOutOfBoundsException:5