chapter17ExceptionHandling_第1页
chapter17ExceptionHandling_第2页
chapter17ExceptionHandling_第3页
chapter17ExceptionHandling_第4页
chapter17ExceptionHandling_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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(“打印结果不正常!请联系管理员打印结果不正常!请联系管理员); finally System.out.println(“continue); TomMikeJohn打印结果不正常!请联系管理员打印结果不正常!请联系管理员continueYour program Or SoftwareSyntax ErrorCompilerDebug And AnalyseLogic Errortry catchRuntime Error(Exception)17.2 Exceptions and Exception Types17.2 Exceptions and Exce

6、ption TypesExceptionArithmeticExceptionArithmeticException( (算术异常算术异常, ,例如除数为零例如除数为零) )ArrayIndexOutOfBoundExceptionArrayIndexOutOfBoundException( (数组越界异常数组越界异常) ) RuntimeException免检异常和必检异常免检异常和必检异常17.3 Exception Handling17.3 Exception Handlingtry statements; /可能出现异常的代码段(例如打开文件)可能出现异常的代码段(例如打开文件) ca

7、tch (Exception1 exVar1) /捕获可能出现的异常捕获可能出现的异常Exception1Exception1(文件没找到)(文件没找到) handler for exception1; /捕获捕获Exception1Exception1异常后的处理代码异常后的处理代码 catch (Exception2 exVar2) handler for exception2; . finally statements; /必须执行的操作(关闭文件)必须执行的操作(关闭文件) 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(“打印结果不正常!请联系管理员打印结果不正常!请联系管理员); finally System.out.println(“continue); TomMikeJohn打印结果不正常!请联系管理员打印结果不正常!请联系管理员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.无论发生异常与否,无论发生异常与否,finallyfinally块中的语句必须执行;块中的语句必须执行;2.2.发生异常后,在发生异常后,在trytry块中,发生异常的语句后面的语句都不再执行;块中,发生异常的语句后面的语句都不再执行;3.3.发生异常后,如果异常被捕获到,执行发生异常后,如果异常被捕获到,执行相应的相应的catchcatch块块里的语句,然后里的语句,然后

11、执行执行finallyfinally块块里的语句和里的语句和finallyfinally块后的语句(块后的语句(statement3statement3);如果没有捕获到,只执行);如果没有捕获到,只执行finallyfinally块里的语句,而块里的语句,而statement3statement3将不能执行。将不能执行。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发生异常并且捕获到发生异常并且捕获到执行执行不执行不执行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);发生异常并且捕获到发生异常并且捕获到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);发生异常没有捕获到发生异常没有捕获到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);代码编写时,捕获异常顺序不对代码编写时,捕获异常顺序不对编译器提示:执行不到编译器提示:执行不到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思考打印结果思考打印结果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的功能的功能)参见实验指导书例程参见实验指导书例程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;/用来记录当前账号上的余额用来记录当前账号上的余额 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( 请输入正数!请输入正数! );); 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论