1. 處理例外 exception
try {
檢查例外是否發生的敘述句
…
}
catch (例外的Class 變數名稱){
例外處理工作
…
}
舉例來說
try {
int [] test = new int[5];
test[10] = 80; // 例外, 超出array範圍
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ERROR MESSAGE:" + e + "occurred.");
}
2. 拋出例外 Throw exception
先宣告指定的function是可能會拋出例外的方法,然後在特定情況(if 條件式)下主動拋出例外。
程式範例可以參考 http://caterpillar.onlyfun.net/Gossip/CppGossip/ExceptionHandling.html
沒有留言 :
張貼留言