五章異?!pt課件_第1頁
五章異?!pt課件_第2頁
五章異常 ppt課件_第3頁
五章異?!pt課件_第4頁
五章異?!pt課件_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第五章第五章 異異 常常回想回想l承繼及其承繼及其JAVA實(shí)現(xiàn)實(shí)現(xiàn)l多態(tài)及其多態(tài)及其JAVA實(shí)現(xiàn)實(shí)現(xiàn)l訪問修飾符對(duì)類成員的訪問限制訪問修飾符對(duì)類成員的訪問限制l方法修飾符:方法修飾符:static、final、abstract目的目的l了解異常的概念了解異常的概念l運(yùn)用運(yùn)用 try 塊、塊、catch 塊和塊和 finally 塊處置異常塊處置異常l運(yùn)用多重運(yùn)用多重 catch 塊處置異常塊處置異常l運(yùn)用嵌套運(yùn)用嵌套 try/catch 塊處置異常塊處置異常l運(yùn)用關(guān)鍵字運(yùn)用關(guān)鍵字 throw 和和 throws 處置異常處置異常l運(yùn)用運(yùn)用JAVA編寫和運(yùn)用自定義異常編寫和運(yùn)用自定義異常什么是異

2、常?什么是異常?public class ExceptionRaised public ExceptionRaised() public int calculate( int operand1, int operand2) int result = operand1 / operand2; return result; public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); int result = obj.calculate(9, 0); System.out.println(resu

3、lt); 異常情況異異 常常程序忽然終止并將控制交給操作系統(tǒng)在運(yùn)轉(zhuǎn)時(shí)發(fā)生的錯(cuò)誤在運(yùn)轉(zhuǎn)時(shí)發(fā)生的錯(cuò)誤 IF B IS ZERO GO TO ERRORC = A / BPRINT CGO TO EXITERROR: 處置異常的塊 “以零作除數(shù),代碼導(dǎo)致錯(cuò)誤 DISPLAY EXIT:END處置異常處置異常 2-12-1處置運(yùn)轉(zhuǎn)時(shí)錯(cuò)誤的偽代碼處置運(yùn)轉(zhuǎn)時(shí)錯(cuò)誤的偽代碼手動(dòng)引發(fā)異常指定由方法引發(fā)的異常 tryfinallycatchthrowsthrow處置異常處置異常 2-22-2JavaJava異常類異常類 文件終了文件終了EOFException找不到文件找不到文件FileNotFoundExcep

4、tionI/O 異常的根類異常的根類IOException數(shù)字轉(zhuǎn)化格式異常,比如字符串到數(shù)字轉(zhuǎn)化格式異常,比如字符串到 float 型數(shù)字的轉(zhuǎn)換無效型數(shù)字的轉(zhuǎn)換無效NumberFormatException不能加載所需的類不能加載所需的類ClassNotFoundException方法接納到非法參數(shù)方法接納到非法參數(shù)IllegalArgumentException數(shù)組大小小于或大于實(shí)踐的數(shù)組大小數(shù)組大小小于或大于實(shí)踐的數(shù)組大小ArrayIndexOutOfBoundException嘗試訪問嘗試訪問 null 對(duì)象成員對(duì)象成員NullPointerException許多許多 java.lang

5、 異常的基類異常的基類RuntimeException異常層次構(gòu)造的根類異常層次構(gòu)造的根類Exception算術(shù)錯(cuò)誤情形,如以零作除數(shù)算術(shù)錯(cuò)誤情形,如以零作除數(shù)ArithmeticException線程中斷線程中斷InterruptedException說說 明明異異 常常try try 和和 catch catch 塊塊 2-12-1trycatch異常異常執(zhí)行 catch 后程序繼續(xù)正常運(yùn)轉(zhuǎn)程序控制 引發(fā)代碼塊單單 元元try try 和和 catch catch 塊塊 2-22-2演示:例如 1ltry 和和 catch 塊的用法塊的用法class ExceptionRaised /*

6、構(gòu)造方法. */ public ExceptionRaised() /* * 這個(gè)方法運(yùn)轉(zhuǎn)時(shí)將會(huì)產(chǎn)生一個(gè)異常. * param operand1 除法中的分子 * param operand2 除法中的分母 * return int 前往除法的結(jié)果 */ public int calculate(int operand1, int operand2) int result = operand1 / operand2; return result; public class ArithmeticException /* 構(gòu)造方法. */ public ArithmeticException()

7、public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); try /* 定義變量 result 以存儲(chǔ)結(jié)果. */ int result = obj.calculate(9, 0); System.out.println(result); catch (Exception e) System.err.println(“發(fā)生異常: + e.toString(); e.printStackTrace(); finally finally 塊塊無異常異常trytry、catch catch 和和

8、 finally finally 塊的執(zhí)行流程塊的執(zhí)行流程異常處置塊的普通方式異常處置塊的普通方式try / 要監(jiān)控錯(cuò)誤的代碼塊 methodGeneratingException(); catch (Exception e) / Exception e 的異常處置程序 finally / 在 try 終了前要執(zhí)行的代碼塊 cleanup();多重多重 catch catch 塊塊3-13-1l一段代碼能夠會(huì)生成多個(gè)異常一段代碼能夠會(huì)生成多個(gè)異常l當(dāng)引發(fā)異常時(shí),會(huì)按順序來查看每個(gè)當(dāng)引發(fā)異常時(shí),會(huì)按順序來查看每個(gè) catch 語句,并執(zhí)語句,并執(zhí)行第一個(gè)類型與異常類型匹配的語句行第一個(gè)類型與異常

9、類型匹配的語句l執(zhí)行其中的一條執(zhí)行其中的一條 catch 語句之后,其他的語句之后,其他的 catch 語句將語句將被忽略被忽略try . catch(ArrayIndexOutOfBoundsException e) catch(Exception e) ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeathSQLExceptionRuntimeExceptionNumberFormatException異常類的層次構(gòu)造異常類的層次構(gòu)造lThrowable 具有兩個(gè)子類,它們是具有兩個(gè)子類

10、,它們是lException:處置用戶程序該當(dāng)捕獲的異常情況:處置用戶程序該當(dāng)捕獲的異常情況lError:Error 類的異常為內(nèi)部錯(cuò)誤,因此在正常類的異常為內(nèi)部錯(cuò)誤,因此在正常情況下不期望用戶的程序捕獲它們情況下不期望用戶的程序捕獲它們 AWTError多重多重 catch catch 塊塊3-23-2l運(yùn)用多重運(yùn)用多重 catch 語句時(shí),異常子類一定要位于異語句時(shí),異常子類一定要位于異常父類之前常父類之前 try . catch(Exception e) catch(ArrayIndexOutOfBoundsException e) 多重多重 catch catch 塊塊3-33-3演示

11、:例如 2l多重多重catch的運(yùn)用的運(yùn)用class ExceptionCode /*構(gòu)造方法.*/ protected ExceptionCode() /*這個(gè)方法將將零作除數(shù).*/ public void calculate() try int num = 0; int num1 = 42 / num; catch (Exception e) System.out.println(父類異常 catch 子句); catch (ArithmeticException ae) / 錯(cuò)誤 不能到達(dá)的代碼 System.out.println(這個(gè)子類的父類是 + exception 類,且不能到

12、達(dá)); class UnreachableCode /*構(gòu)造方法.*/ protected UnreachableCode() /* * 類和運(yùn)用程序的獨(dú)一進(jìn)入點(diǎn). * param args 字符串參數(shù)的數(shù)組 */ public static void main(String args) ExceptionCode obj = new ExceptionCode(); obj.calculate(); 嵌套嵌套 try try catch catch 塊塊 假設(shè)內(nèi)層 try 沒有相應(yīng)的 catch,那么檢查外層 catch class NestedException /* 構(gòu)造方法。 */ p

13、rotected NestedException() /* 這個(gè)方法檢測數(shù)字的格式。 * param argument 用于存儲(chǔ) args 的值。 */ public test(String argumnet) try int num = Integer.parseInt(args1); /* 嵌套 try 塊。 */ try int numValue = Integer.parseInt(args0); System.out.println(“args0 + “的平方是 + numValue * numValue); catch (NumberFormatException nb) Syst

14、em.out.println(“不是一個(gè)數(shù)字! ); catch (ArrayIndexOutOfBoundsException ne) System.out.println(“請(qǐng)輸入數(shù)字!); /*main方法*/ public static void main(String args) NestedException obj = new NestedException(); obj.test(args0); 因此需求嵌套異常處置程序運(yùn)用運(yùn)用 throw throw 和和 throws 2-1 throws 2-1 語句語句 3停頓停頓異常處置程序可執(zhí)行程序語句可執(zhí)行程序語句語句語句 1語句

15、語句 2運(yùn)用運(yùn)用 throw throw 和和 throws 2-2throws 2-2處置異常被調(diào)用的方法被調(diào)用的方法調(diào)用方法調(diào)用方法處置異常能夠會(huì)導(dǎo)致異常防止被調(diào)用的方法出現(xiàn)異常并處置異常type calledmethod-name(parameter-list) throws exception-list / body of methodtype callingmethod-name try / statements calledmethod-name();catch(Exception e) /statements用戶自定義異常用戶自定義異常 2-12-1l自定義異常概念自定義異常概念

16、l運(yùn)用自定義異常的時(shí)候運(yùn)用自定義異常的時(shí)候lJavaAPI提供的內(nèi)置異常不一定總能捕獲程序中提供的內(nèi)置異常不一定總能捕獲程序中發(fā)生的一切錯(cuò)誤。有時(shí)會(huì)需求創(chuàng)建用戶自定義異發(fā)生的一切錯(cuò)誤。有時(shí)會(huì)需求創(chuàng)建用戶自定義異常常 l自定義異常需求承繼自定義異常需求承繼Exception 及其子類及其子類class ArraySizeException extends NegativeArraySizeException /* 構(gòu)造方法。 */ ArraySizeException() super(“您傳送的數(shù)組大小非法); 用戶自定義異常用戶自定義異常 2-22-2例如: 例如 6l創(chuàng)建用戶自定義異常創(chuàng)建用

17、戶自定義異常l承繼承繼 Exception 或其子類或其子類 class UserDefinedExceptions /* 構(gòu)造方法. */ protected UserDefinedExceptions() /* * 類和運(yùn)用程序的獨(dú)一入口點(diǎn). * param arg 字符串參數(shù)的數(shù)組 */ public static void main(String arg) ExceptionClass obj = new ExceptionClass(Integer.parseInt(arg0); class ExceptionClass ExceptionClass(int val) size =

18、val; try checkSize(); catch (ArraySizeException e) System.out.println(e); /* 聲明變量以存儲(chǔ)數(shù)組的大小和元素. */ private int size; private int array; /* 檢查數(shù)組長度的方法. * throws 一個(gè) ArraySizeException */ public void checkSize() throws ArraySizeException if (size 0) throw new ArraySizeException(); array = new int3; for (int count = 0; count 3; count+) arraycount = count + 1; 總結(jié)總結(jié)l異常是運(yùn)轉(zhuǎn)時(shí)發(fā)生的錯(cuò)誤異常是運(yùn)轉(zhuǎn)時(shí)發(fā)生的錯(cuò)誤l可以運(yùn)用可以運(yùn)用 try、catch、throw、throws 和

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論