北大计算机系java培训经典课件第4章java异常

上传人:bin****86 文档编号:55157483 上传时间:2018-09-25 格式:PPT 页数:36 大小:101.50KB
返回 下载 相关 举报
北大计算机系java培训经典课件第4章java异常_第1页
第1页 / 共36页
北大计算机系java培训经典课件第4章java异常_第2页
第2页 / 共36页
北大计算机系java培训经典课件第4章java异常_第3页
第3页 / 共36页
北大计算机系java培训经典课件第4章java异常_第4页
第4页 / 共36页
北大计算机系java培训经典课件第4章java异常_第5页
第5页 / 共36页
点击查看更多>>
资源描述

《北大计算机系java培训经典课件第4章java异常》由会员分享,可在线阅读,更多相关《北大计算机系java培训经典课件第4章java异常(36页珍藏版)》请在金锄头文库上搜索。

1、第 4 章 异常,北京大学计算机系 代亚非,2,第4章 异常,4.1 异常的概念 4.2 异常的分类 4.3 捕获异常 4.4 声明异常 4.5 抛出异常 4.6 创造自己的异常 4.7 总结,3,4.1 异常的概念,什么是异常?异常实际上是程序中错误导致中断了正常的指令流的一种事件. 没有处理错误的程序:read-file openTheFile;determine its size;allocate that much memory;closeTheFile; ,4,4.1 异常的概念,以常规方法处理错误openFiles;if (theFilesOpen) determine the l

2、enth of the file;if (gotTheFileLength)allocate that much memory;if (gotEnoughMemory) read the file into memory;if (readFailed) errorCode=-1;else errorCode=-2;else errorCode=-3;else errorCode=-4 ;else errorCode=-5;,5,4.1 异常的概念,观察前面的程序你会发现大部分精力花在出错处理上了. 只把能够想到的错误考虑到,对以外的情况无法处理 程序可读性差 出错返回信息量太少,6,4.1 异

3、常的概念,用异常的形式处理错误 read-File; try openTheFile;determine its size;allocate that much memory;closeTheFile;catch(fileopenFailed) dosomething; catch(sizeDetermineFailed) dosomething;catch(memoryAllocateFailed) dosomething;catch(readFailed) dosomething;catch(fileCloseFailed) dosomething; ,7,4.1 异常的概念,和传统的方法

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

5、Eception e) badURL=true; repaint();,9,4.2 异常的分类,异常是一个对象,它继承自Throwable类,所有的Throwable类的子孙类所产生的对象都是例外. Error:由Java虚拟机生成并抛出,Java程序不做处理. Runtime Exception(被0除等系统错误,数组下标超范围):由系统检测, 用户的Java 程序可不做处理,系统将它们交给缺省的异常处理程序. Exception(程序中的问题,可预知的): Java编译器要求Java程序必须捕获或声明所有的非运行时异常 throw:用户自己产生异常,10,4.2 异常的分类,.,Throw

6、able,Error,Exception,RuntimeException,缺省的异常 处理程序,由用户捕获或 声明并处理,不做处理,用户自己产生的异常,要处理,11,4.3 捕获异常,捕获并处理异常 try /接受监视的程序块,在此区域内发生/的异常,由catch中指定的程序处理; catch(要处理的异常种类和标识符) /处理异常; catch(要处理的异常种类和标识符) /处理异常; ,12,4.3 捕获异常,常见的异常 ArithmeticException ArrayIndexOutOfBandsException ArrayStoreException IOException Fi

7、leNotFoundException NullPointerException MalformedURLException NumberFormatException OutOfMemoryException,如果在使用能够产生异常的方法而没有捕获和处理,将不能通过编译,13,4.3 捕获异常,例:编写Java程序,包含三种异常 算术异常, 字符串越界,数组越界 观察输出信息: 每个异常对象可以直接给出信息,14,4.3 捕获异常,class first_exception public static void main(String args) char c; int a,b=0;int

8、array=new int7; String s=“Hello“;,try a=1/b; catch(ArithmeticException 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);,15,4.3 捕获异

9、常,一定会执行的程序块-finally 异常处理的统一出口 try /常规的代码; catch() /处理异常 finally /不论发生什么异常(或者不发生任何异常),都要执行的部分; ,16,4.3 捕获异常,finally在文件处理时非常有用 try 对文件进行处理的程序; catch(IOException e) /对文件异常进行处理; finally 不论是否发生异常,都关闭文件; ,17,4.4 声明异常,一个方法不处理它产生的异常,而是沿着调用层次向上传递,由调用它的方法来处理这些异常,叫声明异常. 声明异常的方法 在产生异常的方法名后面加上要抛出(throws)的异常的列表 v

10、oid compute(int x)throws ArithmeticException returnType methodName(parameterlist) throws exceptionList,18,4.4 声明异常,例:若因某种原因不想在创建URL的方法中处理异常,public method1() int x;try x=System.in.read();compute(x);catch(IOException ioe) System.out.println(“read error”); catch(ArithmeticException e) System.out.printl

11、n(“devided by 0”); ,public int compute(int x) throws ArithmeticException e) return z=100/x;,19,4.4 声明异常,20,4.4 声明异常,例:说出程序执行结果 public class exception1 void Proc(int sel) throws ArithmeticException,ArrayIndexOutOfBoundsException System.out.println(“In Situation“ + sel );if (sel=0) System.out.println(

12、“no Exception caught“);return;else if(sel=1) int iArray=new int4;iArray10=3; ,21,4.4 声明异常,public static void main(String args) try Proc(0); Proc(1);catch(ArrayIndexOutOfBoundsException e)System.out.println(“Catch“+e); ,c:jview throwsException In Situation 0 no Exception caught In Situation 1 Catch j

13、ava.lang.ArrayIndexOutOfBoundsException:10,22,4.5 抛出异常,抛弃异常: 不是出错产生,而是人为地抛出 throw ThrowableObject; throw new ArithmeticException(); 例:编写程序人为抛出(JavaThrow.prj)ArithmeticException,ArrayIndexOutOfBoundsExceptionStringIndexOutOfBoundsException,A method,Exception,Another method,throw,caught,23,4.5 抛出异常,cl

14、ass JavaThrow public 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 StringIndexOutOfBoundsExcepti

15、on(); catch(StringIndexOutOfBoundsException si) System.out.println(si); ,24,4.6 创造自己的异常,不是由Java系统监测到的异常(下标越界,被0-除等),而是由用户自己定义的异常. 用户定义的异常同样要用try-catch捕获,但必须由用户自己抛出 throw new MyException. 异常是一个类,用户定义的异常必须继承自Throwable或Exception类,建议用Exception类.,25,4.6 创造自己的异常,形如: class MyException extends Exception .;

16、例1 :计算两个数之和,当任意一个数超出范围时,抛出自己的异常,public class NumberRangeException extends Exception public NumberRangeException(String msg) super(msg); ,26,4.6 创造自己的异常,.,public boolean action(Event evt, Object arg) try int answer = CalcAnswer();answerStr = String.valueOf(answer);catch (NumberRangeException e) answerStr = e.getMessage(); repaint();return true; ,

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

当前位置:首页 > 办公文档 > PPT模板库 > 其它

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