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

下載本文檔

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

最新文檔

評(píng)論

0/150

提交評(píng)論