02-04_異常和斷言.ppt_第1頁(yè)
02-04_異常和斷言.ppt_第2頁(yè)
02-04_異常和斷言.ppt_第3頁(yè)
02-04_異常和斷言.ppt_第4頁(yè)
02-04_異常和斷言.ppt_第5頁(yè)
已閱讀5頁(yè),還剩27頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、Java程序設(shè)計(jì),第六章異常處理與斷言,本章教學(xué)目標(biāo),掌握J(rèn)ava中的異常處理機(jī)制 掌握斷言的使用,本章內(nèi)容,Java中的異常處理 斷言,異常,異常情況的出現(xiàn)會(huì)改變正常的程序流程 很多情況可能導(dǎo)致異常,包括硬件故障、資源耗盡以及程序錯(cuò)誤。 當(dāng)Java中出現(xiàn)異常時(shí),稱為一個(gè)異常被“拋出”。負(fù)責(zé)處理異常的代碼稱作“異常處理程序”,它“捕獲”拋出的異常 當(dāng)某個(gè)異常發(fā)生時(shí),需要有一種方法告訴JVM執(zhí)行什么代碼,為此,使用try和catch關(guān)鍵字,如以下形式try /被監(jiān)視區(qū)域,存在可能產(chǎn)生異常的代碼 catch(Exception e) /如果捕獲到該類異常,執(zhí)行這里,Java異常處理機(jī)制,把各種不同

2、類型的異常情況進(jìn)行分類,用Java類來表示異常情況,這種類被稱為異常類。把異常情況表示成異常類,可以充分發(fā)揮類的可擴(kuò)展和可重用的優(yōu)勢(shì)。 異常流程的代碼和正常流程的代碼分離,提高了程序的可讀性,簡(jiǎn)化了程序的結(jié)構(gòu)。 可以靈活的處理異常,如果當(dāng)前方法有能力處理異常,就捕獲并處理它,否則只需拋出異常,由方法調(diào)用者來處理它。,public void work() try 工作8個(gè)小時(shí) /可能會(huì)拋出DiseaseException異常 下班回家 catch(DiseaseException e) 去醫(yī)院看病 ,異常處理,如果一個(gè)方法不想處理異常,可以通過throws 語(yǔ)句將異常拋向上級(jí)調(diào)用方法。 int

3、method1(int x)throws Exception1,Exception2 if(x0)throw new Exception1(); if(x=0)throw new Exception2(); return +x; void method2() throws Exception1,Exception2 /以下代碼可能拋出異常 int a=method1(1); ,異常處理采用堆棧機(jī)制,public class ExTester static int method1(int x)throws Exception if(x0)throw new Exception(x0); retu

4、rn +x; static int method2(int x)throws Exception return method1(x); public static void main(String args)throws Exception System.out.println(method2(-1); ,main(),method2(),method1(),方法調(diào)用堆棧,finally語(yǔ)句,finally語(yǔ)句定義一個(gè)總是被執(zhí)行的代碼塊,而不管有沒有出現(xiàn)異常。,public void work() try 開門 工作8個(gè)小時(shí) /可能會(huì)拋出DiseaseException異常 catch(Dis

5、easeException e) 去醫(yī)院看病; finally 關(guān)門 ,public void work() try 開門 工作8個(gè)小時(shí)/可能會(huì)拋出DiseaseException異常 關(guān)門 catch(DiseaseException e) 去醫(yī)院看病; ,finally語(yǔ)句,finally語(yǔ)句定義一個(gè)總是被執(zhí)行的代碼塊,而不考慮是否出現(xiàn)異常。,public class FinallyTester static int method1(int x)throws Exception if(x0)throw new Exception(x0); return x+; public static

6、void main(String args) try System.out.println(method1(-1); System.out.println(end); catch(Exception e) System.out.println(Wrong); finally System.out.println(Finally); ,異常處理流程,try code1; /可能拋出各種異常 catch(SQLException e) System.out.println(SQLException); catch(IOException e) System.out.println(IOExcept

7、ion); catch(Exception e) System.out.println(Exception); ,異常處理流程,finally語(yǔ)句不被執(zhí)行的唯一情況是程序先執(zhí)行了終止程序的System.exit()方法 public static void main(String args) try System.out.println(Begin); System.exit(0); finally System.out.println(Finally); System.out.println(End); ,示例,打印 Begin Wrong Finally java.lang. Arithm

8、eticException,public class Sample public static void main(String args) throws Exception try System.out.println(Begin); new Sample().method1(0); / 出現(xiàn)ArithmeticException異常 System.exit(0); catch (Exception e) System.out.println(Wrong); throw e; / 如果把此行注釋掉,將得到不同的運(yùn)行結(jié)果 finally System.out.println(Finally);

9、 System.out.println(End); private void method1(int i) System.out.println(4 / i); ,異常處理流程,public static int methodA(int x) try if(x0) throw new Exception(x0); return x+; catch(Exception e) System.out.println(“Wrong); return -100; finally System.out.println(Finally); public static void main(String arg

10、s) System.out.println( methodA(-1)); ,打印 Wrong Finally -100,JDK類庫(kù)中的異常分類,常見異常,ArithmeticException:數(shù)學(xué)異常 int a=12 / 0; /拋出ArithmeticException NullPointerException:空指針異常 Date d= null; System.out.println(d.toString(); /拋出NullPointerException ArrayIndexOutOfBoundsException:下標(biāo)越界異常 int array=new int4; array

11、0=1; array7=1; /拋出ArrayIndexOutOfBoundsException ClassCastException:類型轉(zhuǎn)換異常: Animal animal=new Dog(); Cat cat=(Animal)animal;,運(yùn)行時(shí)異常,RuntimeException類及其子類都稱為運(yùn)行時(shí)異常,這種異常的特點(diǎn)是Java編譯器不會(huì)檢查它,也就是說,當(dāng)程序中可能出現(xiàn)這類異常,即使沒有用try-catch語(yǔ)句捕獲它,也沒有用throws子句聲明拋出它,也會(huì)編譯通過。例如當(dāng)以下divide()方法的參數(shù)b為0,執(zhí)行“a/b”操作時(shí)會(huì)出現(xiàn)ArrithmeticException

12、異常,它屬于運(yùn)行時(shí)異常,Java編譯器不會(huì)檢查它: public int divide(int a,int b) return a/b; /當(dāng)參數(shù)b為0,拋出ArrithmeticException ,區(qū)分運(yùn)行時(shí)異常和受檢查異常,運(yùn)行時(shí)異常表示無(wú)法讓程序恢復(fù)運(yùn)行的異常,導(dǎo)致這種異常的原因通常是由于執(zhí)行了錯(cuò)誤操作。一旦出現(xiàn)了錯(cuò)誤操作,建議終止程序,因此Java編譯器不檢查這種異常。,public void method(int array) for(int i=0;i=array.length;i+) arrayi=1; /當(dāng)i的取值為array.length時(shí),將拋出ArrayIndexOut

13、OfBoundsException ,public void method(int array) for(int i=0;iarray.length;i+) arrayi=1; /不會(huì)拋出ArrayIndexOutOfBoundsException ,修改程序代碼中的錯(cuò)誤,區(qū)分運(yùn)行時(shí)異常和受檢查異常,受檢查異常表示程序可以處理的異常,如果拋出異常的方法本身不能處理它,那么方法調(diào)用者應(yīng)該去處理它,從而使程序恢復(fù)運(yùn)行,不至于終止程序。 如果一個(gè)方法可能出現(xiàn)受檢查異常,要么用try-catch語(yǔ)句捕獲,要么用throws子句聲明將它拋出,否則會(huì)導(dǎo)致編譯錯(cuò)誤。,void method1() throw

14、s IOException /合法 /編譯錯(cuò)誤,必須捕獲或聲明拋出IOException void method2() method1(); /合法,聲明拋出IOException void method3()throws IOException method1(); ,/合法,聲明拋出Exception void method4()throws Exception method1(); /合法,捕獲IOException void method5() try method1(); catch(IOException e) ,用戶定義異常,用戶定義異常是通過擴(kuò)展Exception類或Runti

15、meException來創(chuàng)建的。,class AnswerWrongException extends Exception private int result; public AnswerWrongException (int result) this.result=result; public int getResult() return result; ,用戶定義異常,public class ExceptionTester public static void test(int x,int y,int z)throws AnswerWrongException if(x+y!=z) t

16、hrow new AnswerWrongException(z); System.out.println(x+y+=+z); public static void main(String args) try test(1,2,5); System.out.println(end); catch( AnswerWrongException e) System.out.println(result is wrong:+e.getResult(); e.printStackTrace(); ,獲得異常信息,Exception提供了如下方法: toString() getMessage() print

17、StackTrace(),try test(1,2,5); System.out.println(end); catch( AnswerWrongException e) e.printStackTrace(); ,本章內(nèi)容,Java中的異常處理 斷言,斷言(assert),從Java語(yǔ)言1.4版本加入了斷言,它允許你在開發(fā)期間測(cè)試假設(shè),而不必付出為異常編寫異常處理程序方面的代碼(時(shí)間和程序開銷兩方面),因?yàn)槟慵僭O(shè)一旦開發(fā)完成,并全部部署,這些異常將永遠(yuǎn)不會(huì)發(fā)生,斷言概述,假設(shè)傳遞給方法(methodA)的數(shù)字不會(huì)為負(fù)數(shù),當(dāng)測(cè)試和調(diào)試時(shí),想驗(yàn)證該假設(shè),但又不想在開發(fā)完成時(shí)去除print語(yǔ)句、運(yùn)

18、行時(shí)異常處理或if/else測(cè)試。 如果非??隙ㄗ约旱募僭O(shè),不想花時(shí)間來編寫異常處理代碼,在運(yùn)行時(shí),也不想把if/else留在那里,可以在開發(fā)期間使用斷言測(cè)試假設(shè),當(dāng)程序部署時(shí),斷言代碼實(shí)質(zhì)上會(huì)被清除,不會(huì)產(chǎn)生任何開銷或要跟蹤和刪除的代碼,private void methodA(int num) if(num=0) useNum(num + x); else System.out.println(“error: ”+num); ,使用斷言,斷言不僅使代碼更簡(jiǎn)潔,而且除非它被特別打開(啟用),否則斷言是不活動(dòng)的。 斷言操作非常簡(jiǎn)單,總是斷言某事是true,如果是true,則無(wú)問題,代碼繼續(xù)運(yùn)行。但如果斷言被證實(shí)是錯(cuò)的(false),則在這里拋出AssertionError(不應(yīng)該處理它們),private void methodA(int num) assert(num=0); useNum(num + x) ,private void methodA(int num) useNum(num + x) ,斷言的形式,斷言有兩種形式:簡(jiǎn)單和非常簡(jiǎn)單 非常簡(jiǎn)單:private void do

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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)論