异常ppt课件.ppt

上传人:本田雅阁 文档编号:3285178 上传时间:2019-08-08 格式:PPT 页数:43 大小:230.54KB
返回 下载 相关 举报
异常ppt课件.ppt_第1页
第1页 / 共43页
异常ppt课件.ppt_第2页
第2页 / 共43页
异常ppt课件.ppt_第3页
第3页 / 共43页
异常ppt课件.ppt_第4页
第4页 / 共43页
异常ppt课件.ppt_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《异常ppt课件.ppt》由会员分享,可在线阅读,更多相关《异常ppt课件.ppt(43页珍藏版)》请在三一文库上搜索。

1、Tel:0571-88394222 QQ;106159278,异常,Tel:0571-88394222 QQ;106159278,异常,异常的概念 异常的分类 捕获异常 声明异常 抛出异常 构建自己的异常,Tel:0571-88394222 QQ;106159278,1 异常的概念,什么是异常? 异常实际上是程序中意外导致中断了正常的指令流的一种事件. 总有一些问题是编译时刻预计不到的 能否很好地处理运行时刻的异常情况是一个程序健康的标志 很多程序员普遍缺乏异常处理意识 用户都知道软件没有不出错的,所以要把运行错误报告给用户,而不是试图隐藏,Tel:0571-88394222 QQ;10615

2、9278,1 异常的概念,没有处理错误的程序: public int getInt() byte buf=new byte1024; System.in.read(buf); String str=new String(buf); str=str.trim(); int i=Integer.parseInt(str); ,Tel:0571-88394222 QQ;106159278,1 异常的概念,使用常规方式处理异常时你会发现大部分精力花在出错处理上了. 只把能够想到的错误考虑到,对以外的情况无法处理 程序可读性差(如果要插入一个功能段) 出错返回信息量太少,Tel:0571-8839422

3、2 QQ;106159278,1 异常的概念,用异常的形式处理错误 public int getInt() try byte buf=new byte1024; System.in.read(buf); String str=new String(buf); str=str.trim(); int i=Integer.parseInt(str); catch(IOException e) System.out.println(e); catch(NumberFormatException e) System.out.println(e); ,Tel:0571-88394222 QQ;10615

4、9278,1 异常的概念,和传统的方法比较异常的优点: 1.把错误代码从常规代码中分离出来 2. 把错误传播给调 用堆栈 3. 按错误类型和 错误差别分组 4. 系统提供了对于一些无法预测的错误的捕获和处理 5. 克服了传统方法的错误信息有限的问题,Tel:0571-88394222 QQ;106159278,1 异常的概念,.,class ExcepTest public static void main(String args) int b=0; int a; try a=4/b; catch(ArithmeticException e) System.out.println(“divid

5、ed by 0”); ,try URL url=new URL(http:/ catch(MalformedURLException e) badURL=true; repaint();,Tel:0571-88394222 QQ;106159278,2 异常的分类,在Java语言中,对很多可能出现的异常进行了标准化,并将它们封装成了各种类,统称为异常类。一旦在程序运行过程中发生异常,Java虚拟机就会自动地创建一个相应的异常类对象,并将该对象作为参数抛给处理异常的方法。 异常是一个类,它继承自Throwable类,所有的Throwable类的子孙类所产生的对象都是例外(包括异常和错误). Er

6、ror类:由Java虚拟机生成并抛出,Java程序不做处理. RuntimeException类(被0除等系统错误,数组下标超范围):由系统检测, 用户的Java 程序可不做处理,系统将它们交给缺省的异常处理程序. Exception类(程序中的问题,可预知的): Java编译器要求Java程序必须捕获或声明所有的非运行时异常 throw关键字:用户自己产生异常,Tel:0571-88394222 QQ;106159278,2 异常的分类,.,缺省的异常 处理程序,要处理,Tel:0571-88394222 QQ;106159278,2 异常的分类,Tel:0571-88394222 QQ;1

7、06159278,2 异常的分类,ArrayIndexOutOfBandsException IOException FileNotFoundException MalformedURLException NumberFormatException OutOfMemoryException,如果在使用能够产生异常的方法而没有捕获和处理,或者声明,将不能通过编译,Tel:0571-88394222 QQ;106159278,3 捕获异常,捕获并处理异常 try /接受监视的程序块,在此区域内发生 /的异常,由catch中指定的程序处理; catch(要处理的异常种类和标识符) /处理异常; ca

8、tch(要处理的异常种类和标识符) /处理异常; ,Tel:0571-88394222 QQ;106159278,3 捕获异常,Exception是与编程有关的所有异常类的基类,没有太多的具体信息,不过可以调用它从其基类Throwable继承的方法 String getMessage() String getLocalizedMessage() String toString() Void printStackTrace() Void printStackTrace(PrintStream) Void printStackTrace(java.io.PrintWriter) Throwable

9、 fillinStackTrace(),Tel:0571-88394222 QQ;106159278,3 捕获异常,例:编写Java程序,包含三种异常 算术异常, 字符串越界,数组越界 观察输出信息: 每个异常对象可以直接给出信息,Tel:0571-88394222 QQ;106159278,3 捕获异常,class first_exception public static void main(String args) char c; int a,b=0;int array=new int7; String s=“Hello“;,try a=1/b; catch(ArithmeticExce

10、ption ae) System.out.println(“Catch“+ae);,try array8=0; catch(ArrayIndexOutOfBoundsException ai) System.out.println(“Catch“ +ai);,try c=s.charAt(8); catch(StringIndexOutOfBoundsException se) System.out.println(“Catch “+se); ,Tel:0571-88394222 QQ;106159278,3 捕获异常,一定会执行的程序块-finally 异常处理的统一出口 try /常规的代

11、码; catch() /处理异常 finally /不论发生什么异常(或者不发生任何异常),都要执行的部分; ,Tel:0571-88394222 QQ;106159278,3 捕获异常,finally在文件处理时非常有用 try 对文件进行处理的程序; catch(IOException e) /对文件异常进行处理; finally 不论是否发生异常,都关闭文件; ,Tel:0571-88394222 QQ;106159278,3 捕获异常,class ThreeException extends Exception public class FinallyWorks static int

12、count = 0; public static void main(String args) while(true) try / Post-increment is zero first time: if(count+ = 0) throw new ThreeException(); System.out.println(“No exception“); catch(ThreeException e) System.err.println(“ThreeException“); finally System.err.println(“In finally clause“); if(count

13、= 2) break; / out of “while“ /:,Tel:0571-88394222 QQ;106159278,3 捕获异常,重新抛出异常 catch(Exception e) System.out.println(“An exception was thrown”); throw e; Rethrowing an exception causes it to go to the exception handlers in the next-higher context. Any further catch clauses for the same try block are s

14、till ignored.,Tel:0571-88394222 QQ;106159278,3 捕获异常,public class Rethrowing public static void f() throws Exception System.out.println(“originating the exception in f()“); throw new Exception(“thrown from f()“); public static void g() throws Throwable try f(); catch(Exception e) System.err.println(“

15、Inside g(),e.printStackTrace()“); e.printStackTrace(); throw e; / 17 / throw e.fillInStackTrace(); / 18 ,Tel:0571-88394222 QQ;106159278,3 捕获异常,public static void main(String args) throws Throwable try g(); catch(Exception e) System.err.println( “Caught in main, e.printStackTrace()“); e.printStackTra

16、ce(); /:,Tel:0571-88394222 QQ;106159278,4 声明异常,一个方法不处理它产生的异常,而是沿着调用层次向上传递,由调用它的方法来处理这些异常,叫声明异常. 声明异常的方法 在产生异常的方法名后面加上要抛出(throws)的异常的列表 void compute(int x)throws ArithmeticException returnType methodName(parameter list) throws exceptionList,Tel:0571-88394222 QQ;106159278,例:若因某种原因不想在操作的方法中处理异常,public

17、method1() int x; try x=System.in.read(); compute(x); catch(IOException ioe) System.out.println(“read error”); catch(ArithmeticException e) System.out.println(“devided by 0”); ,public int compute(int x) throws ArithmeticException return z=100/x;,Tel:0571-88394222 QQ;106159278,Tel:0571-88394222 QQ;106

18、159278,例:说出程序执行结果 public class exception1 void Proc(int sel) throws ArithmeticException, ArrayIndexOutOfBoundsException System.out.println(“In Situation“ + sel ); if (sel=0) System.out.println(“no Exception caught“); return; else if(sel=1) int iArray=new int4; iArray10=3; ,Tel:0571-88394222 QQ;10615

19、9278,抛出异常: 不是系统意外产生,而是人为地抛出 throw ThrowableObject; throw new ArithmeticException(); 例:编写程序人为抛出(JavaThrow.java) ArithmeticException, ArrayIndexOutOfBoundsException StringIndexOutOfBoundsException,A method,Exception,Another method,throw,caught,Tel:0571-88394222 QQ;106159278,13.5 抛出异常,class JavaThrow p

20、ublic static void main(String args) ,try throw new ArithmeticException(); catch(ArithmeticException ae) System.out.println(ae); ,try throw new ArrayIndexOutOfBoundsException(); catch(ArrayIndexOutOfBoundsException ai) System.out.println(ai); ,try throw new StringIndexOutOfBoundsException(); catch(St

21、ringIndexOutOfBoundsException si) System.out.println(si); ,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,不是由Java系统监测到的异常(下标越界,被0-除等),而是由用户自己定义的异常. 用户定义的异常同样要用try-catch捕获,但必须由用户自己抛出 throw new MyException. 异常是一个类,用户定义的异常必须继承自Throwable或Exception类,建议用Exception类.,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常

22、,形如: class MyException extends Exception .; 例1 :计算两个数之和,当任意一个数超出范围时,抛出自己的异常,public class NumberRangeException extends Exception public NumberRangeException(String msg) super(msg); ,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,.,public boolean action() try int answer = CalcAnswer(10, 20); answerStr =

23、String.valueOf(answer); catch (NumberRangeException e) answerStr = e.getMessage(); return true; ,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,public int CalcAnswer(int n1, int n2) throws NumberRangeException int answer = 0; if (int1 20) | (int2 20) NumberRangeException e = new NumberRangeException (”

24、Numbers not within the specified range.“); throw e; answer = int1 + int2; return answer; ,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,例2 :在定义银行类时,若取钱数大于余额则作为异常处理(InsufficientFundsException). 思路:产生异常的条件是余额少于取额, 因此是否抛出异常要判断条件 取钱是withdrawal方法中定义的动作,因此在该方法中产生异常. 处理异常安排在调用withdrawal的时候,因此withdrawal方法要声明异

25、常,由上级方法调用 要定义好自己的异常类,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,class Bank private double balance; public Bank(int ba) this.balance = ba; public void deposite(double dAmount) if (dAmount 0.0) balance += dAmount; public void withdrawal(double dAmount) throws InsufficientFundsException if (balance dA

26、mount) throw new InsufficientFundsException(this, dAmount); balance = balance - dAmount; public String show_balance() return new String(“The balance is “ + (int) balance); ,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,public class ExceptionDemo public static void main(String args) Bank ba = new Bank(

27、50); try ba.withdrawal(100); System.out.println(“Withdrawal successful!“); catch (InsufficientFundsException e) e.printStackTrace(); System.out.println(e.excepMessage(); ,Tel:0571-88394222 QQ;106159278,13.6 创造自己的异常,class InsufficientFundsException extends Exception private Bank excepbank; private do

28、uble excepAmount; InsufficientFundsException(Bank ba, double dAmount) excepbank = ba; excepAmount = dAmount; public String excepMessage() String str = excepbank.show_balance()+“n“ + “The withdrawal was “ + excepAmount; return str; ,Tel:0571-88394222 QQ;106159278,13.7 小结,1.一般格式:正常程序和出错处理分离开来 try Java

29、 statement; catch(ExceptionType1 ExceptionObject) Exception1 handling; catch(ExceptionType2 ExceptionObject) Exception2 handling; . finally final handling; / (统一的出口,最终必定要执行) ,Tel:0571-88394222 QQ;106159278,13.7 小结,2.把异常传播给堆栈,沿着被调用的顺序往前寻找,只要找到符合该异常种类彻底异常处理程序,就交给这部分程序去处理,Tel:0571-88394222 QQ;106159278

30、,13.7 小结,3.异常可以人为地抛出,用throw new 语句 4.异常可以是系统已经定义好的,也可以是用户自己定义的 5.用户自己定义的异常一定继承自Throwable或Exception类,Tel:0571-88394222 QQ;106159278,练习,设计程序产生并处理以下异常 NumberFormatException ArrayIndexOutOfBoundsException ClassCastException 创建对象数组。在对其操作时抛出 NullPointerException,Tel:0571-88394222 QQ;106159278,1.编写一个程序,允许用

31、户向一个大小为10的数组输入整型值。程序应通过索引,或者通过指定一个大于0的值来查找数组元素,从而获得数组中的值。程序应处理任何在向数组输入数值或访问数组元素时发生的异常。此外,程序应使用自定义一个NumberNotFoundException异常处理类。 如果试图访问超出数组边界的元素,则捕获ArrayIndexOutOfBoundsException异常,并显示一个合适的错误消息。 2.修改2的程序,创建一个名为DuplicateValueException的异常类,以检测用户是否输入了重复的数。如果用户输入的数已经存在于数组中,则应该抛出一个DuplicateValueException

32、异常。此外,还要显示一个合适的错误信息。在处理完异常后,程序应该能够继续正常的执行。 3.定义一个InvalidInputException类。该类应是Exception的直接子类。 它应指定默认消息“Your input was invalid”,但允许程序员也能够指定某个定制的消息。,附加,Tel:0571-88394222 QQ;106159278,4. 定义一个ExceptionTest类。ExceptionTest类不仅要检测除数是否为0和是否为有效的整数输入,还要确保输入的整数为正数。如果不是,则应该抛出一个InvalidInputException异常,并显示消息“You mus

33、t enter positive number”。程序应该捕获该异常,并显示一个错误消息。 5.从键盘读入一行信息,根据读入的内容来判定是否发生了异常。如果读入的是空串,则抛出EmptyStringException异常;如果读入的内容中包含有数字,则抛出IncludeNumberException异常。提示:程序中用到String的indexOf()方法,它返回所指定的字符在字符串中第一次出现的位置。如果这个位置大于等于0,表名字符串中含有该字符。程序中使用循环来查找是否出现0到9这10个数字。,Tel:0571-88394222 QQ;106159278,联系方式,杭州和盈科技公司 Address:潮王路238号银地大厦2F ,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1