




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第10章異 常,新浪微博:尚硅谷,本章內(nèi)容,第一節(jié) 異常的原理 第二節(jié) 異常的處理 第三節(jié) 聲明和拋出異常 第四節(jié) 自定義異常類,第一節(jié) 異常的原理,異常:在java語(yǔ)言中,將程序執(zhí)行中發(fā)生的不正常情況稱為“異?!?,異常是一種對(duì)程序運(yùn)行過(guò)程中發(fā)生錯(cuò)誤時(shí)進(jìn)行通知的機(jī)制。 如:網(wǎng)絡(luò)連接失敗、用戶輸入錯(cuò)誤或打開文件失敗等。 異常以對(duì)象的形式出現(xiàn) 不同的異常對(duì)象中封裝了相應(yīng)的錯(cuò)誤信息,異 常,1 public class testexception 2 public static void main(string args) 3 system.out.println(hello world!); 4
2、5 int n = integer.parseint(args0); 6 system.out.println(n = + n); 7 8 命令行執(zhí)行: java testexception 164 正常 java testexception abc numberformatexception java testexception arrayindexoutofboundsexception,示例 testexception類,異常的分類,java語(yǔ)言提供了兩大類異常: 編譯時(shí)異常(受檢異常) 運(yùn)行時(shí)異常(非受檢異常) 編譯時(shí)異常:指程序中必須接受檢查和處理的異常,由exception 類表示
3、。 運(yùn)行時(shí)異常:程序中可以不接受檢查和處理,或致命性錯(cuò)誤異常,由runtimeexception類或error類表示,異常的堆棧式拋出機(jī)制,為保證程序正常執(zhí)行,代碼必須對(duì)可能出現(xiàn)的異常進(jìn)行處理,第二節(jié) 異常的處理,常見異常,java.lang.runtimeexception classcastexception arrayindexoutofboundsexception nullpointerexception arithmeticexception 。 java.io.ioexeption filenotfoundexception eofexception java.lang.clas
4、snotfoundexception java.lang.interruptedexception java.io.filenotfoundexception java.sql.sqlexception,異常的處理方式之一:捕獲處理異常,可使用trycatchfinally語(yǔ)句捕獲異常,避免程序不正常終止 語(yǔ)法: try catch ( ) * finally 可使用多個(gè)catch子句捕獲多個(gè)異常,try-catch示例 testexception類,1 public class testexception 2 public static void main(string args) 3 tr
5、y 4 int n = integer.parseint(args0); 5 system.out.println(n = + n); 6 catch (numberformatexception e) 7 system.out.println(捕獲第一個(gè)異常: + e); 8 system.out.println(e.getmessage(); 9 10 system.out.println(hello world! - end); 11 12,多catch示例 testexception類,1 public class testexception 2 public static void
6、main(string args) 3 try 4 int n = integer.parseint(args0); 5 system.out.println(n = + n); 6 catch (numberformatexception e) 7 system.out.println(捕獲第一個(gè)異常: + e); 8 system.out.println(e.getmessage(); 9 catch (arrayindexoutofboundsexception e) 10 system.out.println(捕獲第二個(gè)異常: + e); 11 catch (exception e)
7、12 system.out.println(“捕獲其他可能的所有異常: + e); 13 14 system.out.println(hello world! - end); 15 16,finally示例 testexception類,1 public class testexception 2 public static void main(string args) 3 try 4 int n = integer.parseint(args0); 5 system.out.println(n = + n); 6 catch (numberformatexception e) 7 syste
8、m.out.println(捕獲第一個(gè)異常: + e); 8 system.out.println(e.getmessage(); 9 return; 10 catch (arrayindexoutofboundsexception e) 11 system.out.println(捕獲第二個(gè)異常: + e); 12 catch (exception e) 13 system.out.println(捕獲其他可能的所有異常: + e); 14 finally /保證該塊的語(yǔ)句總是被執(zhí)行 15 system.out.println(最終塊); 16 17 system.out.println(h
9、ello world! - end); 18 19,練 習(xí),編寫testexception類,在main方法中接收兩個(gè)命令行參數(shù),將它們轉(zhuǎn)換為整數(shù),并用第二個(gè)數(shù)除以第一個(gè)數(shù),打印結(jié)果。 在命令行運(yùn)行程序,給出兩個(gè)參數(shù),測(cè)試以下情況,觀察運(yùn)行結(jié)果: 其中某個(gè)參數(shù)不是數(shù)字 第二個(gè)參數(shù)為0 提示: 分別接收兩個(gè)參數(shù)的語(yǔ)句為: int n1 = integer.parseint(args0); int n2 = integer.parseint(args1); 改寫testexception類的main方法,在其中捕獲可能發(fā)生的異常。 運(yùn)行并測(cè)試各種異常情況,確認(rèn)是否均被捕獲,第三節(jié) 聲明拋出異常,聲
10、明拋出異常,聲明拋出異常是java中處理異常的第二種方式 如果一個(gè)方法(中的語(yǔ)句執(zhí)行時(shí))可能生成某種異常,但是并不能確定如何處理這種異常,則此方法應(yīng)顯示地聲明拋出異常,表明該方法將不對(duì)這些異常進(jìn)行處理,而由該方法的調(diào)用者負(fù)責(zé)處理。 在方法聲明中用throws語(yǔ)句可以聲明拋出異常的列表,throws后面的異常類型可以是方法中產(chǎn)生的異常類型,也可以是它的父類。 聲明拋出異常舉例: public void readfile(string file) throws filenotfoundexception / 讀文件的操作可能產(chǎn)生filenotfoundexception類型的異常 fileinpu
11、tstream fis = new fileinputstream(file); .,聲明拋出異常(2,import java.io.*; public class test6_4 public static void main(string args) test6_4 t = new test6_4(); try t.readfile(); catch(ioexception e) public void readfile() throws ioexception fileinputstream in=new fileinputstream(myfile.txt); int b; b = i
12、n.read(); while(b!= -1) system.out.print(char)b); b = in.read(); in.close();,方法體內(nèi)可能拋出非運(yùn)行時(shí)異常 調(diào)用聲明拋出非運(yùn)行時(shí)異常的方法,聲明拋出異常(3,重寫方法聲明拋出異常的原則,重寫方法不能拋出比被重寫方法范圍更大的異常類型。在多態(tài)的情況下,對(duì)methoda()方法的調(diào)用-異常的捕獲按父類聲明的異常處理,public class a public void methoda() throws ioexception public class b1 extends a public void methoda() t
13、hrows filenotfoundexception public class b2 extends a public void methoda() throws exception /報(bào)錯(cuò),人工拋出異常,java異常類對(duì)象除在程序執(zhí)行過(guò)程中出現(xiàn)異常時(shí)由系統(tǒng)自動(dòng)生成并拋出,也可根據(jù)需要人工創(chuàng)建并拋出。 首先要生成異常類對(duì)象,然后通過(guò)throw語(yǔ)句實(shí)現(xiàn)拋出操作(提交給java運(yùn)行環(huán)境)。 ioexception e = new ioexception(); throw e; 可以拋出的異常必須是throwable或其子類的實(shí)例。下面的語(yǔ)句在編譯時(shí)將會(huì)產(chǎn)生語(yǔ)法錯(cuò)誤: throw new stri
14、ng(want to throw,第四節(jié) 自定義異常類,創(chuàng)建自定義異常類,一般地,用戶自定義異常類都是runtimeexception的子類。 自定義異常類通常需要編寫幾個(gè)重載的構(gòu)造器。 自定義的異常通過(guò)throw拋出。 自定義異常最重要的是異常類的名字,當(dāng)異常出現(xiàn)時(shí),可以根據(jù)名字判斷異常類型,用戶自定義異常類myexception,用于描述數(shù)據(jù)取值范圍錯(cuò)誤信息。用戶自己的異常類必須繼承現(xiàn)有的異常類。 class myexception extends exception static final long serialversionuid = 1l; private int idnumber
15、; public myexception(string message, int id) super(message); this.idnumber = id; public int getid() return idnumber;,創(chuàng)建自定義異常類,使用自定義異常類,public class test6_5 public void regist(int num) throws myexception if (num 0) throw new myexception(“人數(shù)為負(fù)值,不合理”, 3); else system.out.println(登記人數(shù) + num ); public vo
16、id manager() try regist(100); catch (myexception e) system.out.print(登記失敗,出錯(cuò)種類+e.getid(); system.out.print(本次登記操作結(jié)束); public static void main(string args) test6_5 t = new test6_5(); t.manager();,異常處理5個(gè)關(guān)鍵字,捕獲異常,拋出異常,聲明異常,try,catch,finally,執(zhí)行可能產(chǎn)生異常的代碼,捕獲異常,無(wú)論是否發(fā)生異常,代碼總被執(zhí)行,throw,異常的生成階段:手動(dòng)拋出異常對(duì)象,throws
17、,異常的處理方式:聲明方法可能要拋出的各種異常類,例如:上游排污,下游治污,練習(xí)3,public class returnexceptiondemo static void methoda() try system.out.println(進(jìn)入方法a); throw new runtimeexception(制造異常); finally system.out.println(用a方法的finally); static void methodb() try system.out.println(進(jìn)入方法b); return; finally system.out.println(調(diào)用b方法的fi
18、nally);,public static void main(string args) try methoda(); catch (exception e) system.out.println(e.getmessage(); methodb();,判斷程序的輸出結(jié)果,練習(xí)4,編寫應(yīng)用程序ecmdef.java,接收命令行的兩個(gè)參數(shù),要求不能輸入負(fù)數(shù),計(jì)算兩數(shù)相除。 對(duì)數(shù)據(jù)類型不一致(numberformatexception)、缺少命令行參數(shù)(arrayindexoutofboundsexception、 除0(arithmeticexception)及輸入負(fù)數(shù)(ecdef 自定義的異常)
19、進(jìn)行異常處理。 提示: (1)在主類(ecmdef)中定義異常方法(ecm)完成兩數(shù)相除功能。 (2)在main()方法中使用異常處理語(yǔ)句進(jìn)行異常處理。 (3)在程序中,自定義對(duì)應(yīng)輸入負(fù)數(shù)的異常類(ecdef)。 (4)運(yùn)行時(shí)接受參數(shù) java ecmdef 20 10 /args0=“20” args1=“10” (5)interger類的static方法parseint(string s)將s轉(zhuǎn)換成對(duì)應(yīng)的int值。如int a=interger.parseint(“314”);/a=314,異常處理機(jī)制(1,在編寫程序時(shí),經(jīng)常要在可能出現(xiàn)錯(cuò)誤的地方加上檢測(cè)的代碼,如進(jìn)行x/y運(yùn)算時(shí),要檢測(cè)分母為0,數(shù)據(jù)為空,輸入的不是數(shù)據(jù)而是字符等。過(guò)多的分支會(huì)導(dǎo)致程序的代碼加長(zhǎng),可讀性差。因此采用異常機(jī)制。 java異常處理 java采用異常處理機(jī)制,將異常處理的程序代碼集中在一起,與正常的程序代碼分開,使得程序簡(jiǎn)潔,并易于維護(hù),異常處理機(jī)制(2,java提供的是異常處理的抓拋模型。 java程序的執(zhí)行過(guò)程中如出現(xiàn)異常,會(huì)生成一個(gè)異常類對(duì)象,該異常對(duì)象將被提交給java運(yùn)行時(shí)系統(tǒng),這個(gè)過(guò)程稱為拋出(throw)異常。 異常對(duì)象的生成 由虛擬機(jī)自動(dòng)生成:程序
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 藝術(shù)品租賃合同
- 會(huì)議場(chǎng)地租賃合同協(xié)議書
- 保密協(xié)議商業(yè)合同
- 無(wú)錫工藝職業(yè)技術(shù)學(xué)院《工程安全健康與環(huán)境管理》2023-2024學(xué)年第二學(xué)期期末試卷
- 寧夏民族職業(yè)技術(shù)學(xué)院《賓館酒店管理》2023-2024學(xué)年第二學(xué)期期末試卷
- 菏澤家政職業(yè)學(xué)院《輕工行業(yè)清潔生產(chǎn)及污染控制技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- Unit 5 Revealing Nature Developing ideas The Secret Language of Plants教學(xué)設(shè)計(jì) 2024-2025學(xué)年高中英語(yǔ)人教版選擇性必修第二冊(cè)
- 沈陽(yáng)醫(yī)學(xué)院《機(jī)器人工程專業(yè)導(dǎo)論》2023-2024學(xué)年第二學(xué)期期末試卷
- 寧夏財(cái)經(jīng)職業(yè)技術(shù)學(xué)院《主題閱讀(1)》2023-2024學(xué)年第二學(xué)期期末試卷
- 山東勝利職業(yè)學(xué)院《化工原理一》2023-2024學(xué)年第二學(xué)期期末試卷
- 2023年司法鑒定程序通則
- 2023年高中音樂(lè)課件陽(yáng)關(guān)三疊(琴歌)
- 胸腔閉式引流護(hù)理-中華護(hù)理學(xué)會(huì)團(tuán)體標(biāo)準(zhǔn)
- 公司資產(chǎn)情況說(shuō)明范文百度
- 醫(yī)療器械中有害殘留物檢驗(yàn)技術(shù)
- 2015-2022年大慶醫(yī)學(xué)高等??茖W(xué)校高職單招語(yǔ)文/數(shù)學(xué)/英語(yǔ)筆試參考題庫(kù)含答案解析
- 產(chǎn)品過(guò)程特殊特性初始清單(示例)
- 兩篇古典英文版成語(yǔ)故事塞翁失馬
- 中國(guó)古代文學(xué)史 馬工程課件(中)13第五編 宋代文學(xué) 第一章 北宋初期文學(xué)
- GB/T 14643.4-2009工業(yè)循環(huán)冷卻水中菌藻的測(cè)定方法第4部分:土壤真菌的測(cè)定平皿計(jì)數(shù)法
- DL-T 5190.1-2022 電力建設(shè)施工技術(shù)規(guī)范 第1部分:土建結(jié)構(gòu)工程(附條文說(shuō)明)
評(píng)論
0/150
提交評(píng)論