/**
- 異常測試
- @author bo
*/
public class Excep {
public static void main(String[]args) {
try {
//要檢查的程序語句;程序員感覺 到這句可能會出錯
int tempArr[] = new int[5];//聲明一個存儲五個int類型的數(shù)組
tempArr [7] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
//異常發(fā)生時的處理語句
System.out.println("數(shù)組 超出了存儲的范圍!!!");
System.out.println("異常的內(nèi)容:"+e); //顯示出異常的內(nèi)容
}finally {
System.out.println("這里執(zhí)行到了...");
//一定會運行到的語句
}
TestExp();
System.out.println("main 函數(shù) 運行結(jié)束");
}
//拋出異常
public static void TestExp(){
//除數(shù) 不能為 0
int a = 4 ,b = 0;
try {
if (b == 0) {
// throw 關(guān)鍵字所拋出的 是異常類的實例對象 因此必須使用new 產(chǎn)生對象
throw new ArithmeticException("除數(shù)不能為0 出現(xiàn)算術(shù)異常");
}else{
System.out.println(a+"/"+b+"="+a/b);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("拋出的異常為:"+e);
}
}
}