孙卫琴《java面向对象编程》配套ppt--java_base3_exception

上传人:kms****20 文档编号:50933917 上传时间:2018-08-11 格式:PPT 页数:18 大小:122.50KB
返回 下载 相关 举报
孙卫琴《java面向对象编程》配套ppt--java_base3_exception_第1页
第1页 / 共18页
孙卫琴《java面向对象编程》配套ppt--java_base3_exception_第2页
第2页 / 共18页
孙卫琴《java面向对象编程》配套ppt--java_base3_exception_第3页
第3页 / 共18页
孙卫琴《java面向对象编程》配套ppt--java_base3_exception_第4页
第4页 / 共18页
孙卫琴《java面向对象编程》配套ppt--java_base3_exception_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《孙卫琴《java面向对象编程》配套ppt--java_base3_exception》由会员分享,可在线阅读,更多相关《孙卫琴《java面向对象编程》配套ppt--java_base3_exception(18页珍藏版)》请在金锄头文库上搜索。

1、第5课 异常处理nJava异常处理机制n把各种不同类型的异常情况进行分类,用Java类来表示异常情况, 这种类被称为异常类。把异常情况表示成异常类,可以充分发挥类 的可扩展和可重用的优势。n异常流程的代码和正常流程的代码分离,提高了程序的可读性,简 化了程序的结构。n可以灵活的处理异常,如果当前方法有能力处理异常,就捕获并处 理它,否则只需抛出异常,由方法调用者来处理它。参见Java面向对象编程的第9章public void work() try工作8个小时 /可能会抛出DiseaseException异常下班回家catch(DiseaseException e)去医院看病 异常处理n在Jav

2、a编程语言中,用try和catch语句来 处理异常。格式如下:1. try 2. / code that might throw a particular exception 3. catch (SpecialException e) 4. / code to execute if a SpecialException is thrown 5. catch (Exception e) 6. / code to execute if a general Exception exception is thrown 7. 异常处理n如果一个方法不想处理异常,可以通过 throws 语句将异常抛向上级

3、调用方法。int method1(int x)throws Exception1,Exception2 if(x0)throw new Exception1();if(x=0)throw new Exception2();return +x; void method2() throws Exception1,Exception2 /以下代码可能抛出异常int a=method1(1); 异常处理采用堆栈机制public class ExTesterstatic int method1(int x)throws Exceptionif(x0)throw new Exception(“x0“);r

4、eturn +x;static int method2(int x)throws Exceptionreturn method1(x);public static void main(String args)throws ExceptionSystem.out.println(method2(-1); main()method2()method1()方法调用堆栈finally语句nfinally语句定义一个总是被执行的代码块,而不管有没有出现异常。public void work() try开门工作8个小时 /可能会抛出DiseaseException异常catch(DiseaseExcept

5、ion e)去医院看病;finally关门 public void work() try开门工作8个小时/可能会抛出DiseaseException异常关门catch(DiseaseException e)去医院看病; finally语句nfinally语句定义一 个总是被执行的 代码块,而不考 虑是否出现异常 。public class FinallyTesterstatic int method1(int x)throws Exceptionif(x0)throw new Exception(“x0“);return x+;public static void main(String ar

6、gs)trySystem.out.println(method1(-1);System.out.println(“end“);catch(Exception e)System.out.println(“Wrong“);finallySystem.out.println(“Finally“);异常处理流程trycode1; /可能抛出各种异常 catch(SQLException e)System.out.println(“SQLException“); catch(IOException e)System.out.println(“IOException“); catch(Exception

7、e)System.out.println(“Exception“); 异常处理流程nfinally语句不被执行的唯一情况是程序先执行 了终止程序的System.exit()方法public static void main(String args)trySystem.out.println(“Begin“);System.exit(0);finallySystem.out.println(“Finally“);System.out.println(“End“); 异常处理流程public static void main(String args)throws ExceptiontrySyste

8、m.out.println(“Begin“);new Sample().method1(-1); /出现SpecialException异常System.exit(0);catch(Exception e)System.out.println(“Wrong“);throw e; /如果把此行注释掉,将得到不同的运行结果finallySystem.out.println(“Finally“);System.out.println(“End“); 打印 Begin Wrong Finallyjava.lang.SpecialException 异常处理流程public static int met

9、hod1(int x)tryif(x0)throw new Exception(“x0“);return x+;catch(Exception e)System.out.println(“Wrong“);return -100;finallySystem.out.println(“Finally“);public static void main(String args)System.out.println( method1(-1)); 打印 Wrong Finally -100JDK类库中的异常分类常见异常nArithmeticException:数学异常 int a=12 / 0; /抛出

10、ArithmeticExceptionnNullPointerException:空指针异常 Date d= null; System.out.println(d.toString(); /抛出NullPointerExceptionnArrayIndexOutOfBoundsException:下标越界异常 int array=new int4; array0=1; array7=1; /抛出ArrayIndexOutOfBoundsExceptionnClassCastException:类型转换异常: Animal animal=new Dog(); Cat cat=(Animal)an

11、imal;运行时异常 nRuntimeException类及其子类都称为运行时异常,这 种异常的特点是Java编译器不会检查它,也就是说, 当程序中可能出现这类异常,即使没有用try-catch语 句捕获它,也没有用throws子句声明抛出它,也会编 译通过。例如当以下divide()方法的参数b为0,执行 “a/b”操作时会出现ArrithmeticException异常,它属于 运行时异常,Java编译器不会检查它: public int divide(int a,int b)return a/b; /当参数b为0,抛出ArrithmeticException 区分运行时异常和受检查异常n

12、运行时异常表示无法让程序恢复运行的异常,导致这种异常的原 因通常是由于执行了错误操作。一旦出现了错误操作,建议终止 程序,因此Java编译器不检查这种异常。 public void method(int array)for(int i=0;i=array.length;i+)arrayi=1; /当i的取值为array.length时,将抛出ArrayIndexOutOfBoundsException public void method(int array)for(int i=0;iarray.length;i+)arrayi=1; /不会抛出ArrayIndexOutOfBoundsExc

13、eption 修改程序代码中的错误区分运行时异常和受检查异常n受检查异常表示程序可以处理的异常,如果抛出异常的方法本身不能处 理它,那么方法调用者应该去处理它,从而使程序恢复运行,不至于终 止程序。 n如果一个方法可能出现受检查异常,要么用try-catch语句捕获,要么用throws子句声明将它抛出,否则会导致编译错误。 void method1() throws IOException /合法/编译错误,必须捕获或声明抛出IOException void method2()method1(); /合法,声明抛出IOException void method3()throws IOExcep

14、tion method1(); /合法,声明抛出Exception void method4()throws Exception method1(); /合法,捕获IOException void method5()trymethod1(); catch(IOException e) 用户定义异常n用户定义异常是通过扩展Exception类或 RuntimeException来创建的。class AnswerWrongException extends Exception private int result;public AnswerWrongException (int result)th

15、is.result=result;public int getResult() return result;用户定义异常(参见ExceptionTester.java )public class ExceptionTesterpublic static void test(int x,int y,int z)throws AnswerWrongExceptionif(x+y!=z) throw new AnswerWrongException(z);System.out.println(x+“+“+y+“=“+z); public static void main(String args) trytest(1,2,5);System.out.println(“end“);catch( AnswerWrongException e)System.out.println(“result is wrong:“+e.getResult();e.printStackTrace(); 获得异常信息Exception提供了如下方法 :n toString()n getMessage()n printStackTrace()trytest(1,2,5);System.out.println(“

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号