chapter17ExceptionHandling_第1頁
chapter17ExceptionHandling_第2頁
chapter17ExceptionHandling_第3頁
chapter17ExceptionHandling_第4頁
chapter17ExceptionHandling_第5頁
已閱讀5頁,還剩33頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

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、碼編寫階段代碼編寫階段代碼編譯階段代碼編譯階段代碼運行階段代碼運行階段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é)果不正常!請聯(lián)系管理員打印結(jié)果不正常!請聯(lián)系管理員); finally System.out.println(“continue); TomMikeJohn打印結(jié)果不正常!請聯(lián)系管理員打印結(jié)果不正常!請聯(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)異常的代碼段(例如打開文件)可能出現(xiàn)異常的代碼段(例如打開文件) ca

7、tch (Exception1 exVar1) /捕獲可能出現(xiàn)的異常捕獲可能出現(xiàn)的異常Exception1Exception1(文件沒找到)(文件沒找到) 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é)果不正常!請聯(lián)系管理員打印結(jié)果不正常!請聯(lián)系管理員); finally System.out.println(“continue); TomMikeJohn打印結(jié)果不正常!請聯(lián)系管理員打印結(jié)果不正常!請聯(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.無論發(fā)生異常與否,無論發(fā)生異常與否,finallyfinally塊中的語句必須執(zhí)行;塊中的語句必須執(zhí)行;2.2.發(fā)生異常后,在發(fā)生異常后,在trytry塊中,發(fā)生異常的語句后面的語句都不再執(zhí)行;塊中,發(fā)生異常的語句后面的語句都不再執(zhí)行;3.3.發(fā)生異常后,如果異常被捕獲到,執(zhí)行發(fā)生異常后,如果異常被捕獲到,執(zhí)行相應(yīng)的相應(yīng)的catchcatch塊塊里的語句,然后里的語句,然后

11、執(zhí)行執(zhí)行finallyfinally塊塊里的語句和里的語句和finallyfinally塊后的語句(塊后的語句(statement3statement3);如果沒有捕獲到,只執(zhí)行);如果沒有捕獲到,只執(zhí)行finallyfinally塊里的語句,而塊里的語句,而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ā)生異常沒有捕獲到發(fā)生異常沒有捕獲到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);代碼編寫時,捕獲異常順序不對代碼編寫時,捕獲異常順序不對編譯器提示:執(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 異常類()異常類()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 異常類名稱異常類名稱 extends Exception Note:1.Throug the custom Exception, the software can describe its ownerror, because these error doesnt defined in the java. (通過自定通過自定義異常的方式,軟件可以描述自身的錯誤。有很多實際的錯誤在義異常的方式,軟件可以描述自身的錯誤。有很多實際的錯誤在j

21、ava中并沒有定義。中并沒有定義。)2.We can doesnt code anything, Only with inheritance from the Exception. (我們可以對自定義的異常不編寫任何代碼,只要從我們可以對自定義的異常不編寫任何代碼,只要從Exception繼承即繼承即可,充分使用可,充分使用Exception的功能的功能)參見實驗指導(dǎo)書例程參見實驗指導(dǎo)書例程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;/用來記錄當(dāng)前賬號上的余額用來記錄當(dāng)前賬號上的余額 public BankAccount(double moneyCount) this.moneyCount = moneyCount; public void getMoney(double money)try if (money moneyCount)

27、 throw new InsufficientMoneyException(錢不夠了錢不夠了!); if (money (money moneyCountmoneyCount) ) throw throw newnew InsufficientMoneyException( InsufficientMoneyException( 錢不夠了錢不夠了!);); if if (money 0) (money 0) throw throw newnew InsufficientMoneyException( InsufficientMoneyException( 請輸入正數(shù)!請輸入正數(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論