java典型應(yīng)用徹查1000例第一卷入門源碼習(xí)題bookjavavol-1javach_第1頁(yè)
java典型應(yīng)用徹查1000例第一卷入門源碼習(xí)題bookjavavol-1javach_第2頁(yè)
java典型應(yīng)用徹查1000例第一卷入門源碼習(xí)題bookjavavol-1javach_第3頁(yè)
java典型應(yīng)用徹查1000例第一卷入門源碼習(xí)題bookjavavol-1javach_第4頁(yè)
java典型應(yīng)用徹查1000例第一卷入門源碼習(xí)題bookjavavol-1javach_第5頁(yè)
已閱讀5頁(yè),還剩17頁(yè)未讀 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論