java的異常處理 exception.ppt_第1頁
java的異常處理 exception.ppt_第2頁
java的異常處理 exception.ppt_第3頁
java的異常處理 exception.ppt_第4頁
java的異常處理 exception.ppt_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、T12 異常,回顧,區(qū)分 Java 應(yīng)用程序和 Java Applet Applet 的生命周期 如何將參數(shù)傳遞給 Applet 如何在 Applet中插入多媒體文件,2,目標(biāo),異常的概念 運(yùn)用 try 、catch 和 finally 處理異常 throw 和 throws 編寫和使用自定義異常,3,什么是異常?,4,public class ExceptionRaised public ExceptionRaised() public int calculate( int operand1, int operand2) int result = operand1 / operand2; r

2、eturn result; public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); int result = obj.calculate(9, 0); System.out.println(result); ,異常情況,異 常,程序突然終止并將控制交給操作系統(tǒng),在運(yùn)行時發(fā)生的錯誤,處理異常:傳統(tǒng)思路,5, IF B = 0 GO TO ERROR C = A / B PRINT C GO TO EXIT ERROR: 處理異常的塊 “以零作除數(shù),代碼導(dǎo)致錯誤” DISPLAY EXIT:

3、 END,處理運(yùn)行時錯誤的偽代碼,處理異常,6,手動引發(fā)異常,指定方法所忽略的異常,try,finally,catch,throws,throw,Java異常類,7,文件結(jié)束,EOFException,找不到文件,FileNotFoundException,I/O 異常的根類,IOException,數(shù)字轉(zhuǎn)化格式異常,比如字符串到 float 型數(shù)字的轉(zhuǎn)換無效,NumberFormatException,不能加載所需的類,ClassNotFoundException,方法接收到非法參數(shù),IllegalArgumentException,數(shù)組大小小于或大于實(shí)際的數(shù)組大小,ArrayIndexOu

4、tOfBoundException,嘗試訪問 null 對象成員,NullPointerException,許多 java.lang 異常的基類,RuntimeException,異常層次結(jié)構(gòu)的根類,Exception,算術(shù)錯誤情形,如以零作除數(shù),ArithmeticException,線程中斷,InterruptedException,說 明,異 常,使用try和catch捕獲異常,8,try,catch,異常,執(zhí)行 catch 后程序 繼續(xù)正常運(yùn)行,程序控制,引發(fā),代碼塊,單 元,try和catch代碼示例,9,演示:示例 1,try 和 catch 塊的用法,class Exceptio

5、nRaised /* 構(gòu)造方法. */ public ExceptionRaised() /* * 這個方法運(yùn)行時將會產(chǎn)生一個異常. * 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 Arithmetic

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

7、h 塊,無異常,異常,try、catch 和 finally 塊的執(zhí)行流程,異常處理的一般形式,try / 要監(jiān)控錯誤的代碼塊 methodGeneratingException(); catch (Exception e) / Exception e 的異常處理程序 finally / 在 try 結(jié)束前要執(zhí)行的代碼塊 cleanup(); ,11,多重 catch,一段代碼可能會生成多個異常 當(dāng)引發(fā)異常時,會按順序來查看每個 catch 語句,并執(zhí)行第一個類型與異常類型匹配的語句 執(zhí)行其中的一條 catch 語句之后,其他的 catch 語句將被忽略,12,try . catch(Arra

8、yIndexOutOfBoundsException e) catch(Exception e) ,異常類的繼承結(jié)構(gòu),Throwable 具有兩個子類,它們是 Exception:處理用戶程序應(yīng)當(dāng)捕獲的異常情況 Error:Error 類的異常為內(nèi)部錯誤,因此在正常情況下不期望用戶的程序捕獲它們,13,Exception,ArithmeticException,NullPointerException,Object,Throwable,Error,ThreadDeath,SQLException,RuntimeException,NumberFormatException,AWTError,多

9、重catch中的順序問題,使用多重 catch 語句時,異常子類一定要位于異常父類之前,14,try . catch(Exception e) catch(ArrayIndexOutOfBoundsException e) ,多重catch中的順序問題,15,演示:示例 2,多重catch的使用,class ExceptionCode /*構(gòu)造方法.*/ protected ExceptionCode() /*這個方法將將零作除數(shù).*/ public void calculate() try int num = 0; int num1 = 42 / num; catch (Exception

10、e) System.out.println(父類異常 catch 子句); catch (ArithmeticException ae) / 錯誤 不能到達(dá)的代碼 System.out.println(這個子類的父類是 + exception 類,且不能到達(dá)); ,class UnreachableCode /*構(gòu)造方法.*/ protected UnreachableCode() /* * 類和應(yīng)用程序的唯一進(jìn)入點(diǎn). * param args 字符串參數(shù)的數(shù)組 */ public static void main(String args) ExceptionCode obj = new Ex

11、ceptionCode(); obj.calculate(); ,嵌套 try catch,16,如果內(nèi)層 try 沒有相應(yīng)的 catch,則檢查外層 catch,class NestedException /* 構(gòu)造方法。 */ protected NestedException() /* 這個方法檢測數(shù)字的格式。 * param argument 用于存儲 args 的值。 */ public test(String argumnet) try int num = Integer.parseInt(args1); /* 嵌套 try 塊。 */ try int numValue = Int

12、eger.parseInt(args0); System.out.println(“args0 + “的平方是 + numValue * numValue); catch (NumberFormatException nb) System.out.println(“不是一個數(shù)字! ); catch (ArrayIndexOutOfBoundsException ne) System.out.println(“請輸入數(shù)字!); /*main方法*/ public static void main(String args) NestedException obj = new NestedExcep

13、tion(); obj.test(args0); ,因此需要嵌套 異常處理程序,使用throw拋出異常,17,語句 3,throw 異常,引發(fā)的異常,停止,異常處理程序,可執(zhí)行程序語句,語句 1,語句 2,throw和throws的比較,18,處理異常,被調(diào)用的方法,調(diào)用方法,處理異常,忽略可能的異常,防止出現(xiàn) 異常并處理異常,type calledmethod-name (parameter-list) throws exception-list / body of method ,type callingmethod-name try / statements calledmethod-n

14、ame(); catch(Exception e) /statements ,用戶自定義異常,自定義異常概念 使用自定義異常的時候 JavaAPI提供的內(nèi)置異常不一定總能表達(dá)程序中發(fā)生的所有錯誤。有時會需要創(chuàng)建用戶自定義異常 自定義異常需要繼承Exception 及其子類,19,創(chuàng)建和使用用戶自定義異常,20,class ArraySizeException extends NegativeArraySizeException /* 構(gòu)造方法。 */ ArraySizeException() super(“您傳遞的數(shù)組大小非法); ,示例: 示例 6,創(chuàng)建用戶自定義異常 繼承 Exceptio

15、n 或其子類,class ExceptionClass ExceptionClass(int val) size = val; try checkSize(); catch (ArraySizeException e) System.out.println(e); /* 聲明變量以存儲數(shù)組的大小和元素. */ private int size; private int array; /* 檢查數(shù)組長度的方法. * throws 一個 ArraySizeException */ public void checkSize() throws ArraySizeException if (size

16、0) throw new ArraySizeException(); array = new int3; for (int count = 0; count 3; count+) arraycount = count + 1; ,class UserDefinedExceptions /* 構(gòu)造方法. */ protected UserDefinedExceptions() /* * 類和應(yīng)用程序的唯一入口點(diǎn). * param arg 字符串參數(shù)的數(shù)組 */ public static void main(String arg) ExceptionClass obj = new ExceptionClass

溫馨提示

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

評論

0/150

提交評論