根據(jù)最新版的課程描述(Course Description Effective 2014 fall),會(huì)出現(xiàn)在考試中的Exceptions(異常),只有5種,如下表所示:
表格中的5個(gè)exceptions都屬于runtime error;所謂的runtime error是指:運(yùn)行程序時(shí),當(dāng)環(huán)境檢測(cè)到一個(gè)不可能被執(zhí)行的操作,就會(huì)出現(xiàn)運(yùn)行錯(cuò)誤(runtime error)。
下面,我們就分別來(lái)看一下這5種在考試中可能會(huì)出現(xiàn)的Exceptions。
1. ArithmeticException 常見(jiàn)的ArithmeticException就是除數(shù)為0,比如:
當(dāng)你運(yùn)行以上程序時(shí),系統(tǒng)就會(huì)出現(xiàn): “exception ArithmeticException”。
2. NullPointerException NullPointerException 也屬于runtime error。
一般來(lái)說(shuō),當(dāng)你試圖使用一個(gè) null reference variable 來(lái)調(diào)用方法(method)時(shí),就會(huì)出現(xiàn)NullPointerException,比如:
當(dāng)你運(yùn)行以上程序時(shí),系統(tǒng)就會(huì)提示: “exception NullPointerException” 3. IndexOutOfBoundsException 當(dāng)程序在運(yùn)行時(shí),試圖用非法索引(index)訪問(wèn)動(dòng)態(tài)數(shù)組時(shí)(ArrayList),系統(tǒng)就會(huì)報(bào)錯(cuò),比如:
當(dāng)你運(yùn)行以上程序時(shí),系統(tǒng)就會(huì)提示: “exception IndexOutOfBoundsException”。
4. ArrayIndexOutOfBoundsException 當(dāng)程序在運(yùn)行時(shí),試圖用非法索引(index)訪問(wèn)數(shù)組時(shí)(array),系統(tǒng)就會(huì)報(bào)錯(cuò),比如:
當(dāng)你運(yùn)行以上程序時(shí),系統(tǒng)就會(huì)提示: “exception ArrayIndexOutOfBoundsException”。
這里再說(shuō)一個(gè)類似的exception: StringIndexOutOfBoundsException,顧名思義,這個(gè)exception是發(fā)生在String身上的,比如:
當(dāng)你運(yùn)行以上程序時(shí),系統(tǒng)就會(huì)提示:“exception StringIndexOutOfBoundsException”。
5. IllegalArgumentException 當(dāng)程序在運(yùn)行時(shí),調(diào)用的方法(method)或者構(gòu)造器(contructor)中的參數(shù)(parameter),不符合該方法或者構(gòu)造器對(duì)參數(shù)的具體要求時(shí),系統(tǒng)就會(huì)報(bào)錯(cuò),比如:
由于Color這個(gè)構(gòu)造器中的三個(gè) int 型參數(shù)的范圍都必須在 0-255 的范圍之內(nèi),所以當(dāng)上述程序運(yùn)行時(shí),系統(tǒng)就會(huì)提示:“exception IllegalArgumentException”。
--------- AP Computer Science A 考前刷題精講班: 上課時(shí)間:2018年4月14日(周六)、15日(周日)、21日(周六)、22日(周日)
上課方式:網(wǎng)絡(luò)直播課,可回看
課時(shí):每次2小時(shí),總計(jì)8小時(shí) 費(fèi)用:2400元(參加過(guò)AP CS A 課程的老學(xué)員8折優(yōu)惠)
適合學(xué)員:有AP CS A課程基礎(chǔ),準(zhǔn)備2018年5月份參加AP考試的同學(xué)。
講題范圍:2013年至2017年一共5年的Free Response Questions,共計(jì)18道大題。
增值服務(wù):給所有報(bào)名學(xué)員提供兩套小幫手原創(chuàng)AP CS A模考選擇題,并提供答疑服務(wù)。

? 2025. All Rights Reserved. 滬ICP備2023009024號(hào)-1