版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第九章異常處理(Exceptions)9-1 簡(jiǎn)介執(zhí)行Java程序時(shí),若遇到錯(cuò)誤程序代碼,Java即以異常處理機(jī)制處理,并顯示錯(cuò)誤警語(yǔ),提醒程序設(shè)計(jì)師有關(guān)事宜。有關(guān)重大錯(cuò)誤,由系統(tǒng)內(nèi)的錯(cuò)誤警告例程執(zhí)行,一般性錯(cuò)誤,則由程序設(shè)計(jì)師自定義警告程序執(zhí)行。系統(tǒng)內(nèi)建的錯(cuò)誤警告例程,可參考附錄A,包括:java.lang的Error、java.lang的Exception、java.Util的Exception、java.io的Exception、java.awt的Exception、 的Exception。9-2 try/catch/finally代碼塊在Java類別程序塊內(nèi)(除了一般程序代碼區(qū)域外)
2、可分割成數(shù)個(gè)特定意義的代碼塊。凡影響系統(tǒng)作業(yè)的程序代碼必須要在“try代碼塊”執(zhí)行。如果try代碼塊有引發(fā)編譯錯(cuò)誤的程序代碼或引發(fā)錯(cuò)誤的邏輯行為,則由“catch代碼塊”捕捉其錯(cuò)誤,顯示其錯(cuò)誤信息及停止程序繼續(xù)執(zhí)行?!癴inally代碼塊”的程序代碼為必須要執(zhí)行的流程。圖 9-1trycatchfinallycompiling1yes2yes3may work4no5no6no7no8may work范例129:創(chuàng)建網(wǎng)站與try/catch的應(yīng)用01 import .*;02 import java.io.*; 03 public class Ex09_2_1_1 04 ServerSocke
3、t SS; 05 public Ex09_2_1_1() 06 SS = new ServerSocket(1234);07 System.out.println(Server created.);08 09 public static void main(String args)10 Ex09_2_1_1 ServerStart=new Ex09_2_1_1();11 12 范例130:創(chuàng)建網(wǎng)站與try/catch的應(yīng)用01 import .*;02 import java.io.*; 03 public class Ex09_2_1_2 04 ServerSocket SS; 05 pub
4、lic Ex09_2_1_2() 06 try07 SS = new ServerSocket(1234);08 System.out.println(Server created.);09 10 catch(IOException e)11 System.out.println(e.getMessage();12 13 14 public static void main(String args)15 Ex09_2_1_2 ServerStart=new Ex09_2_1_2();16 17 范例131:不設(shè)try/catch代碼塊的應(yīng)用01 class Ex09_2_2_1 02 publ
5、ic static void main (String args)03 04 int x; 05 x = 10 / 0;06 System.out.println(x= + x);07 08 范例132:try/catch代碼塊與異常事件數(shù)據(jù)庫(kù)的應(yīng)用01 class Ex09_2_2_2 02 public static void main (String args) 03 int x; 04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 catch (ArithmeticException e) 09 System.out.pri
6、ntln(In ArithmeticException:+e.getMessage();10 11 12 范例133:catch代碼塊以Exception取代ArithmeticException的應(yīng)用01 class Ex09_2_2_3 02 public static void main (String args) 03 int x;04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 catch (Exception e) 09 System.out.println(In Exception :+e.getMessage();
7、10 11 12 范例134:finally代碼塊的應(yīng)用 01 class Ex09_2_3_1 02 public static void main (String args) 03 int x; 04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 catch (Exception e) 09 System.out.println(In Exception :+e.getMessage();10 11 finally 12 System.out.println(In finally);13 14 15 范例135:依圖9-1項(xiàng)3,
8、設(shè)置try代碼塊,不設(shè)置catch代碼塊,如果加置finally代碼塊也可能編譯成功01 class Ex09_2_3_2 02 public static void main (String args) 03 int x;04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 09 范例136:依圖9-2-1項(xiàng)3,設(shè)置try代碼塊,不設(shè)置catch代碼塊,如果加置finally代碼塊也可能編譯不成功 01 import .*;02 import java.io.*; 03 public class Ex09_2_3_3 04 Serv
9、erSocket SS;05 public Ex09_2_3_3() 06 try07 SS = new ServerSocket(1234);08 System.out.println(Server created.);09 10 public static void main(String args)11 Ex09_2_3_3 ServerStart=new Ex09_2_3_3();12 13 9-3 throws的用法Java使用try/catch代碼塊捕捉異常事件,尤其在處理重要事件時(shí),為了系統(tǒng)安全,更是要求程序代碼必須要置于try/catch代碼塊內(nèi)執(zhí)行,否則將發(fā)生編譯錯(cuò)誤。因此,
10、程序設(shè)計(jì)師在設(shè)計(jì)程序時(shí),必須先考慮是否要使用try/catch代碼塊。為了減少程序設(shè)計(jì)師的困擾,可在方法程序(Methods) 或構(gòu)造函數(shù)(Constructor) 聲明“throws”,以取代try/catch的使用。范例137:方法程序或構(gòu)造函數(shù)聲明 “throws” 以取代try/catch的使用。 01 import .*;02 import java.io.*; 03 public class Ex09_3_1 04 ServerSocket SS; 05 public Ex09_3_1() throws IOException 06 SS = new ServerSocket(12
11、34);07 System.out.println(Server created.);08 09 public static void main(String args) throws IOException10 Ex09_3_1 ServerStart=new Ex09_3_1();11 12 范例138:方法程序或構(gòu)造函數(shù)聲明“throws”以取代try/catch的使用01 class Ex09_3_2 02 public static void main (String args) throws ArithmeticException 03 int x;04 x = 10 / 0;05
12、 System.out.println(x= + x);06 07 9-4 自定義異常處理對(duì)象自定義異常處理對(duì)象可分以下兩種。以內(nèi)建異常處理類別產(chǎn)生自定義新對(duì)象。自定義異常處理類別產(chǎn)生自定義新對(duì)象。范例139:自定義匿名新異常處理對(duì)象 01 class Ex09_4_1_1 02 public static void main (String args) 03 int x, y;04 try 05 x = 10;06 y = 0;07 if(y=0) throw new ArithmeticException(DIY Message);08 x = 10 / 0;09 System.out.p
13、rintln(x= + x);10 11 catch (ArithmeticException e)12 System.out.println(In ArithmeticException:+e.getMessage();13 14 15 范例140:自定義具體新異常處理對(duì)象01 class Ex09_4_1_2 02 public static void main (String args) 03 ArithmeticException f = new ArithmeticException(DIY Message);04 int x, y;05 try 06 x = 10;07 y = 0
14、;08 x = 10 / 0;09 System.out.println(x= + x);10 11 catch (ArithmeticException e)12 System.out.println(In Built Message: +e.getMessage();13 System.out.println(In DIY Message: +f.getMessage();14 15 16 范例141:自定義預(yù)定信息異常處理類與匿名新對(duì)象的應(yīng)用01 class myException extends Exception 02 myException() 03 super(myExcepti
15、on Message);04 05 06 class Ex09_4_2_1 07 public static void main (String args) 08 int x, y;09 try 10 x = 10;11 y = 0;12 if(y=0) throw new myException();13 x = 10 / 0;14 System.out.println(x= + x);15 16 catch (myException e)17 System.out.println(In myException: +e.getMessage();18 19 20 范例142:自定義預(yù)定信息異
16、常處理類與具體新對(duì)象的應(yīng)用01 class myException extends Exception 02 myException() 03 super(myException Message);04 05 06 class Ex09_4_2_2 07 public static void main (String args) 08 myException f = new myException();09 int x, y;10 try 11 x = 10;12 y = 0;13 x = 10 / 0;14 System.out.println(x= + x);15 16 catch (Ar
17、ithmeticException e)17 System.out.println(In Built Message: +e.getMessage();18 System.out.println(In myException Message: +f.getMessage();19 20 21 范例143:自定義信息異常處理類與匿名新對(duì)象的應(yīng)用01 class myException extends Exception 02 myException(String msg) 03 super(msg);04 05 06 class Ex09_4_2_3 07 public static void
18、main (String args) 08 int x, y; 09 try 10 x = 10;11 y = 0;12 if(y=0) throw new myException(DIY Message);13 x = 10 / 0;14 System.out.println(x= + x);15 16 catch (myException e)17 System.out.println(In myException: +e.getMessage();18 19 20 范例144:自定義信息異常處理類與具體新對(duì)象的應(yīng)用01 class myException extends Exception 02 myException(String msg) 03 super(msg);04 05 06 class Ex09_4_2_4 07 public static void main (String args) 08
溫馨提示
- 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ù)覽,若沒有圖紙預(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年天津電子信息職業(yè)技術(shù)學(xué)院?jiǎn)握芯C合素質(zhì)筆試備考題庫(kù)含詳細(xì)答案解析
- 2026年浙江機(jī)電職業(yè)技術(shù)學(xué)院?jiǎn)握芯C合素質(zhì)考試備考試題含詳細(xì)答案解析
- 2026年湖南外國(guó)語(yǔ)職業(yè)學(xué)院?jiǎn)握芯C合素質(zhì)筆試參考題庫(kù)含詳細(xì)答案解析
- 安徽醫(yī)科大學(xué)《中國(guó)近現(xiàn)代史綱要IV》2024-2025學(xué)年期末試卷(A卷)
- 深度解析(2026)《YBT 6320-2024 鋼鐵生產(chǎn)企業(yè)碳資產(chǎn)管理規(guī)范》
- 2026年教育機(jī)構(gòu)教師筆試模擬卷
- 醫(yī)療健康數(shù)據(jù)隱私保護(hù)技巧分享經(jīng)驗(yàn)總結(jié)
- 安檢防爆面試題目及答案
- 計(jì)算機(jī)基礎(chǔ)知識(shí)試題題庫(kù)附答案解析
- 身份識(shí)別查對(duì)制度及答案
- 裝修工程施工質(zhì)量檢查標(biāo)準(zhǔn)
- 供銷大集:中國(guó)供銷商貿(mào)流通集團(tuán)有限公司擬對(duì)威海集采集配商貿(mào)物流有限責(zé)任公司增資擴(kuò)股所涉及的威海集采集配商貿(mào)物流有限責(zé)任公司股東全部權(quán)益價(jià)值資產(chǎn)評(píng)估報(bào)告
- 干細(xì)胞臨床研究:知情同意的倫理審查要點(diǎn)
- 檢測(cè)實(shí)驗(yàn)室安全管理與操作規(guī)程
- 2025云南保山電力股份有限公司招聘(100人)筆試歷年參考題庫(kù)附帶答案詳解
- (新教材)2026年人教版八年級(jí)下冊(cè)數(shù)學(xué) 21.1 四邊形及多邊形 課件
- 教師職業(yè)行為規(guī)范手冊(cè)
- 急性胸痛患者的快速識(shí)別與護(hù)理配合
- 法律研究與實(shí)踐
- 單招第四大類考試試題及答案
- 《建設(shè)工程總承包計(jì)價(jià)規(guī)范》
評(píng)論
0/150
提交評(píng)論