版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Chapter17 Exception HandlingCompile Source Codee.g. javac HelloWorld.javaSouce Code FileSaved on the diske.g. HelloWorld.javaCreate/Modify Source CodeByte Code FileSaved on the diskHelloWorld.classRun Byte code,Loaded to the memorye.g. java HelloWorldresultIf have errorsIf the result is not correct代
2、碼編寫(xiě)階段代碼編寫(xiě)階段代碼編譯階段代碼編譯階段代碼運(yùn)行階段代碼運(yùn)行階段17.1 Introduce17.1 Introducepublic class Testpublic static void main(String args)int m = 26;String n = 3;int p = m/n;System.out.println(the reuslt is : + p);Type mismatch: cannot convert from int to StringThe operator / is undefined for the argument type(s) int, St
3、ringCompilerpublic class Test public static void main(String args) String friends = Tom, Mike“, John; for(int i=0; i1; i+) System.out.println(friendsi); The result is errorpublic class Test public static void main(String args) String friends = Tom, Mike,John; for(int i=0; i4; i+) System.out.println(
4、friendsi); TomMikeJohnException in thread main java.lang.ArrayIndexOutOfBoundsException: 3at Test.main(Test.java:7)public class Test public static void main(String args) String friends = Tom, Mike,John; try for(int i=0; i4; i+) System.out.println(friendsi); catch (ArrayIndexOutOfBoundsException e) S
5、ystem.out.println(“打印結(jié)果不正常!請(qǐng)聯(lián)系管理員打印結(jié)果不正常!請(qǐng)聯(lián)系管理員); finally System.out.println(“continue); TomMikeJohn打印結(jié)果不正常!請(qǐng)聯(lián)系管理員打印結(jié)果不正常!請(qǐng)聯(lián)系管理員continueYour program Or SoftwareSyntax ErrorCompilerDebug And AnalyseLogic Errortry catchRuntime Error(Exception)17.2 Exceptions and Exception Types17.2 Exceptions and Exce
6、ption TypesExceptionArithmeticExceptionArithmeticException( (算術(shù)異常算術(shù)異常, ,例如除數(shù)為零例如除數(shù)為零) )ArrayIndexOutOfBoundExceptionArrayIndexOutOfBoundException( (數(shù)組越界異常數(shù)組越界異常) ) RuntimeException免檢異常和必檢異常免檢異常和必檢異常17.3 Exception Handling17.3 Exception Handlingtry statements; /可能出現(xiàn)異常的代碼段(例如打開(kāi)文件)可能出現(xiàn)異常的代碼段(例如打開(kāi)文件) ca
7、tch (Exception1 exVar1) /捕獲可能出現(xiàn)的異常捕獲可能出現(xiàn)的異常Exception1Exception1(文件沒(méi)找到)(文件沒(méi)找到) handler for exception1; /捕獲捕獲Exception1Exception1異常后的處理代碼異常后的處理代碼 catch (Exception2 exVar2) handler for exception2; . finally statements; /必須執(zhí)行的操作(關(guān)閉文件)必須執(zhí)行的操作(關(guān)閉文件) public class Test public static void main(String args) S
8、tring friends = Tom, Mike,John; try for(int i=0; i4; i+) System.out.println(friendsi); catch (ArrayIndexOutOfBoundsException e) System.out.println(“打印結(jié)果不正常!請(qǐng)聯(lián)系管理員打印結(jié)果不正常!請(qǐng)聯(lián)系管理員); finally System.out.println(“continue); TomMikeJohn打印結(jié)果不正常!請(qǐng)聯(lián)系管理員打印結(jié)果不正常!請(qǐng)聯(lián)系管理員continuetry statements; / Statements that m
9、ay throw exceptions catch (Exception ex) try statements; catch (Exception ex) catch (RuntimeException ex) try statements; catch (RuntimeException ex) catch (Exception ex) an exception occurs no exception occurs try statement1; statement2;catch(SomeException1 e) catch(SomeException2 e) finally statem
10、ent3try statement1; statement2;catch(SomeException1 e) catch(SomeException2 e) finally statement3Note3Note3:1.1.無(wú)論發(fā)生異常與否,無(wú)論發(fā)生異常與否,finallyfinally塊中的語(yǔ)句必須執(zhí)行;塊中的語(yǔ)句必須執(zhí)行;2.2.發(fā)生異常后,在發(fā)生異常后,在trytry塊中,發(fā)生異常的語(yǔ)句后面的語(yǔ)句都不再執(zhí)行;塊中,發(fā)生異常的語(yǔ)句后面的語(yǔ)句都不再執(zhí)行;3.3.發(fā)生異常后,如果異常被捕獲到,執(zhí)行發(fā)生異常后,如果異常被捕獲到,執(zhí)行相應(yīng)的相應(yīng)的catchcatch塊塊里的語(yǔ)句,然后里的語(yǔ)句,然后
11、執(zhí)行執(zhí)行finallyfinally塊塊里的語(yǔ)句和里的語(yǔ)句和finallyfinally塊后的語(yǔ)句(塊后的語(yǔ)句(statement3statement3);如果沒(méi)有捕獲到,只執(zhí)行);如果沒(méi)有捕獲到,只執(zhí)行finallyfinally塊里的語(yǔ)句,而塊里的語(yǔ)句,而statement3statement3將不能執(zhí)行。將不能執(zhí)行。int a = 0, 1, 2, 3, 4 ;try for (int i = 0; i 100; i+) ai = i+1; catch (ArrayIndexOutOfBoundsException e) System.out.println(This is ArrayI
12、ndexOutOfBoundException); catch (Exception e) System.out.println(This is Excetpiton); finally System.out.println(Execute finish);System.out.println(continue);This is ArrayIndexOutOfBoundExceptionExecute finishcontinue發(fā)生異常并且捕獲到發(fā)生異常并且捕獲到執(zhí)行執(zhí)行不執(zhí)行不執(zhí)行int a = 0, 1, 2, 3, 4 ;try for (int i = 0; i 100; i+) a
13、i = i+1; catch (Exception e) System.out.println(This is Excetpiton); finally System.out.println(Execute finish);System.out.println(continue);發(fā)生異常并且捕獲到發(fā)生異常并且捕獲到This is ExcetpitonExecute finishcontinueint a = 0, 1, 2, 3, 4 ;try for (int i = 0; i 100; i+) ai = i+1; catch (ArithmeticException e) System.
14、out.println(This is ArithmeticException);finally System.out.println(Execute finish);System.out.println(continue);發(fā)生異常沒(méi)有捕獲到發(fā)生異常沒(méi)有捕獲到Execute finishException in thread “main” :ArrayIndexOutOfBoundsException:int a = 0, 1, 2, 3, 4 ;try for (int i = 0; i 100; i+) ai = i+1; catch (Exception e) System.out.p
15、rintln(This is Excetpiton); catch (ArrayIndexOutOfBoundsException e) System.out.println(This is ArrayIndexOutOfBoundException); finally System.out.println(Execute finish);System.out.println(continue);代碼編寫(xiě)時(shí),捕獲異常順序不對(duì)代碼編寫(xiě)時(shí),捕獲異常順序不對(duì)編譯器提示:執(zhí)行不到編譯器提示:執(zhí)行不到ArrayIndexOutOfBoundsExceptionint a = 0, 1, 2, 3, 4
16、;try for (int i = 0; i 100; i+) ai = i+1; System.out.println(“for loop end!); catch (ArrayIndexOutOfBoundsException e) System.out.println(ArrayIndexOutOfBoundException); catch (Exception e) System.out.println(Excetpiton); finally System.out.println(Execute finish);System.out.println(continue);ArrayI
17、ndexOutOfBoundExceptionExecute finishcontinue思考打印結(jié)果思考打印結(jié)果throw new 異常類(lèi)()異常類(lèi)()trytry int int m = 100, n=0; m = 100, n=0; int int a = m/n; a = m/n; catchcatch(ArithmeticException e)(ArithmeticException e) System. System.outout.println(.println(“errorerror);); trytry int int m = 100, n=0; m = 100, n=0;
18、 if if (n=0) (n=0) throw throw newnew ArithmeticException(); ArithmeticException(); int int a = m/n; a = m/n; catchcatch (ArithmeticException e) (ArithmeticException e) System. System.outout.println(.println(“errorerror);); public void m1() throws SomeExceptionpublic void m2() throws SomeException1,
19、SomeException2public void m3() try m1(); catch (SomeException e) process SomeException public class Test public static void main(String args) try int m = 100, n=0; Test t = new Test(); t.division(m, n); catch (ArithmeticException e) System.out.println(“error); public int division(int a, int b)throws
20、 ArithmeticException return a/b; 17.4 Custom Exception17.4 Custom Exceptionclass 異常類(lèi)名稱異常類(lèi)名稱 extends Exception Note:1.Throug the custom Exception, the software can describe its ownerror, because these error doesnt defined in the java. (通過(guò)自定通過(guò)自定義異常的方式,軟件可以描述自身的錯(cuò)誤。有很多實(shí)際的錯(cuò)誤在義異常的方式,軟件可以描述自身的錯(cuò)誤。有很多實(shí)際的錯(cuò)誤在j
21、ava中并沒(méi)有定義。中并沒(méi)有定義。)2.We can doesnt code anything, Only with inheritance from the Exception. (我們可以對(duì)自定義的異常不編寫(xiě)任何代碼,只要從我們可以對(duì)自定義的異常不編寫(xiě)任何代碼,只要從Exception繼承即繼承即可,充分使用可,充分使用Exception的功能的功能)參見(jiàn)實(shí)驗(yàn)指導(dǎo)書(shū)例程參見(jiàn)實(shí)驗(yàn)指導(dǎo)書(shū)例程public class MyException extends Exceptionpublic class CustomExceptionExample public static void main(S
22、tring args) try throw new MyException(); catch (Exception e) System.out.println(e); System.out.println(Its caught!); finally System.out.println(Its finally!); MyExceptionIts caught!Its finally!打印的是異常的名稱打印的是異常的名稱public class MyException extends Exception public MyException(String message) super(messa
23、ge) public class CustomExceptionExample public static void main(String args) try throw new MyException(“test“); catch (Exception e) System.out.println(e); System.out.println(Its caught!); finally System.out.println(Its finally!); MyException:testIts caught!Its finally!打印的是異常的名稱和打印的是異常的名稱和自定義的消息提示自定義
24、的消息提示自定義異常的消息提示自定義異常的消息提示public class Test public static void main(String args) System.out.println(Enter your age:); Scanner scanner = new Scanner(System.in); int age = scanner.nextInt(); Test test = new Test(); test.checkAge(age); public boolean checkAge(int age) boolean result = false; try if(age
25、200) throw new AgeException(age is bigger!);result = true; catch (AgeException e) System.out.println(e); return result; public class AgeException extends Exception public AgeException() public AgeException(String message) super(message);public class InsufficientMoneyException extends Exception publi
26、c InsufficientMoneyException() public InsufficientMoneyException(String message) super(message);public class BankAccount private double moneyCount;/用來(lái)記錄當(dāng)前賬號(hào)上的余額用來(lái)記錄當(dāng)前賬號(hào)上的余額 public BankAccount(double moneyCount) this.moneyCount = moneyCount; public void getMoney(double money)try if (money moneyCount)
27、 throw new InsufficientMoneyException(錢(qián)不夠了錢(qián)不夠了!); if (money (money moneyCountmoneyCount) ) throw throw newnew InsufficientMoneyException( InsufficientMoneyException( 錢(qián)不夠了錢(qián)不夠了!);); if if (money 0) (money 0) throw throw newnew InsufficientMoneyException( InsufficientMoneyException( 請(qǐng)輸入正數(shù)!請(qǐng)輸入正數(shù)! );); S
28、ystem. System.outout.println(.println( 取款成功取款成功!);); publicpublic classclass TestBankCount TestBankCount public public staticstatic voidvoid main(String args) main(String args) Scanner s = Scanner s = newnew Scanner(System. Scanner(System.inin);); doubledouble money = s.nextDouble(); money = s.nextDouble(); BankAccount bankAccount = BankAccount bankAccount = newnew BankAccount(1000); BankAccount(1000); try try bankAccount.getMoney(money); bankAccount.getMoney(money); catchcatch (InsufficientMoneyException e)
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025服裝連鎖加盟合同樣本
- 2025海上運(yùn)輸合同模板書(shū)
- 二零二五年度車(chē)輛轉(zhuǎn)讓與道路救援服務(wù)合同3篇
- 二零二五年度股權(quán)投資公司股東合作協(xié)議3篇
- 二零二五年度文化產(chǎn)業(yè)發(fā)展全新期權(quán)合同3篇
- 2025年度養(yǎng)羊產(chǎn)業(yè)人才培養(yǎng)與交流合作協(xié)議3篇
- 二零二五年度生態(tài)保護(hù)公益合作合同3篇
- 2025年度虛擬現(xiàn)實(shí)合伙人股權(quán)分配與內(nèi)容開(kāi)發(fā)合同3篇
- 二零二五年度生態(tài)農(nóng)業(yè)用地農(nóng)村房屋買(mǎi)賣(mài)合同協(xié)議書(shū)
- 2025年度農(nóng)村自建房包工與智能安防系統(tǒng)安裝合同
- 大班春季班級(jí)工作計(jì)劃下學(xué)期
- 2024年重慶鐵路投資集團(tuán)有限公司招聘筆試沖刺題(帶答案解析)
- 研學(xué)教育項(xiàng)目商業(yè)計(jì)劃書(shū)
- MOOC 創(chuàng)新思維與創(chuàng)業(yè)實(shí)驗(yàn)-東南大學(xué) 中國(guó)大學(xué)慕課答案
- 新生兒先心病篩查工作計(jì)劃
- 新能源汽車(chē)研發(fā)合作協(xié)議書(shū)
- 四川省成都市2023-2024學(xué)年高二上學(xué)期期末校級(jí)調(diào)研聯(lián)考數(shù)學(xué)試題【含答案解析】
- 4s店管理的年度工作總結(jié)
- 中醫(yī)護(hù)理查房脅痛好
- 新概念英語(yǔ)第一冊(cè)1-72課測(cè)試
- 類(lèi)風(fēng)濕關(guān)節(jié)炎課件
評(píng)論
0/150
提交評(píng)論