JAVA语言第五章异常

上传人:max****ui 文档编号:24190756 上传时间:2021-06-25 格式:PPT 页数:21 大小:466KB
收藏 版权申诉 举报 下载
JAVA语言第五章异常_第1页
第1页 / 共21页
JAVA语言第五章异常_第2页
第2页 / 共21页
JAVA语言第五章异常_第3页
第3页 / 共21页
资源描述:

《JAVA语言第五章异常》由会员分享,可在线阅读,更多相关《JAVA语言第五章异常(21页珍藏版)》请在装配图网上搜索。

1、第五章异 常 回顾q继承及其JAVA实现q多态及其JAVA实现q访问修饰符对类成员的访问限制q方法修饰符:static、final、abstract 目标q理解异常的概念q运用 try 块、catch 块和 finally 块处理异常q运用多重 catch 块处理异常q运用嵌套 try/catch 块处理异常q运用关键字 throw 和 throws 处理异常q运用JAVA编写和使用自定义异常 什么是异常?public class ExceptionRaised public ExceptionRaised() public int calculate( int operand1, int o

2、perand2) int result = operand1 / operand2; return result; public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); int result = obj.calculate(9, 0); System.out.println(result); 异常情况异 常程序突然终止并将控制交给操作系统在运行时发生的错误 IF B IS ZERO GO TO ERRORC = A / BPRINT CGO TO EXITERROR: 处理异常的块 “

3、以零作除数,代码导致错误” DISPLAY EXIT:END处理异常 2-1处理运行时错误的伪代码 手动引发异常指定由方法引发的异常 tryfinally catchthrowsthrow 处理异常 2-2要监控的程序语句包含在此块中以合理的方式捕获和处理异常释放资源等 Java异常类 文件结束EOFException找不到文件FileNotFoundException I/O 异常的根类IOException数字转化格式异常,比如字符串到 float 型数字的转换无效NumberFormatException不能加载所需的类ClassNotFoundException方法接收到非法参数Ill

4、egalArgumentException数组大小小于或大于实际的数组大小ArrayIndexOutOfBoundException尝试访问 null 对象成员NullPointerException许多 java.lang 异常的基类RuntimeException 异常层次结构的根类Exception算术错误情形,如以零作除数ArithmeticException 线程中断InterruptedException 说 明异 常 try 和 catch 块 2-1trycatch异常 执行 catch 后程序继续正常运行程序控制 引发代码块单 元 try 和 catch 块 2-2 演示:示

5、例 1qtry 和 catch 块的用法class ExceptionRaised /* 构造方法. */ public ExceptionRaised() /* * 这个方法运行时将会产生一个异常. * param operand1 除法中的分子 * param operand2 除法中的分母 * return int 返回除法的结果 */ public int calculate(int operand1, int operand2) int result = operand1 / operand2; return result; public class ArithmeticExcept

6、ion /* 构造方法. */ public ArithmeticException() public static void main(String args) ExceptionRaised obj = new ExceptionRaised(); try /* 定义变量 result 以存储结果. */ int result = obj.calculate(9, 0); System.out.println(result); catch (Exception e) System.err.println(“发生异常: + e.toString(); e.printStackTrace();

7、 finally 块无异常异常try、catch 和 finally 块的执行流程 异常处理块的一般形式try / 要监控错误的代码块 methodGeneratingException(); catch (Exception e) / Exception e 的异常处理程序 finally / 在 try 结束前要执行的代码块 cleanup(); 多重 catch 块3-1q一段代码可能会生成多个异常q当引发异常时,会按顺序来查看每个 catch 语句,并执行第一个类型与异常类型匹配的语句q执行其中的一条 catch 语句之后,其他的 catch 语句将被忽略 try . catch(Ar

8、rayIndexOutOfBoundsException e) catch(Exception e) ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeath SQLException RuntimeExceptionNumberFormatException异常类的层次结构qThrowable 具有两个子类,它们是qException:处理用户程序应当捕获的异常情况qError:Error 类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们 AWTError 多重 catch 块3

9、-2q使用多重 catch 语句时,异常子类一定要位于异常父类之前 try . catch(Exception e) catch(ArrayIndexOutOfBoundsException e) 多重 catch 块3-3演示:示例 2q多重catch的使用class ExceptionCode /*构造方法.*/ protected ExceptionCode() /*这个方法将将零作除数.*/ public void calculate() try int num = 0; int num1 = 42 / num; catch (Exception e) System.out.print

10、ln(父类异常 catch 子句); catch (ArithmeticException ae) / 错误 不能到达的代码 System.out.println(这个子类的父类是 + exception 类,且不能到达); class UnreachableCode /*构造方法.*/ protected UnreachableCode() /* * 类和应用程序的唯一进入点. * param args 字符串参数的数组 */ public static void main(String args) ExceptionCode obj = new ExceptionCode(); obj.c

11、alculate(); 嵌套 try catch 块 如果内层 try 没有相应的 catch,则检查外层 catch class NestedException /* 构造方法。 */ protected NestedException() /* 这个方法检测数字的格式。 * param argument 用于存储 args 的值。 */ public test(String argumnet) try int num = Integer.parseInt(args1); /* 嵌套 try 块。 */ try int numValue = Integer.parseInt(args0);

12、System.out.println(“args0 + “的平方是 + numValue * numValue); catch (NumberFormatException nb) System.out.println(“不是一个数字! ); catch (ArrayIndexOutOfBoundsException ne) System.out.println(“请输入数字!); /*main方法*/ public static void main(String args) NestedException obj = new NestedException(); obj.test(args0

13、); 因此需要嵌套异常处理程序 使用 throw 和 throws 2-1 语句 3停止异常处理程序可执行程序语句语句 1语句 2 使用 throw 和 throws 2-2 处理异常被调用的方法调用方法处理异常可能会导致异常防止被调用的方法出现异常并处理异常type calledmethod-name(parameter-list) throws exception-list / body of method type callingmethod-name try / statements calledmethod-name();catch(Exception e) /statements

14、用户自定义异常 2-1q自定义异常概念q使用自定义异常的时候qJavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常 q自定义异常需要继承Exception 及其子类 class ArraySizeException extends NegativeArraySizeException /* 构造方法。 */ ArraySizeException() super(“您传递的数组大小非法); 用户自定义异常 2-2示例: 示例 6q创建用户自定义异常q继承 Exception 或其子类 class ExceptionClass ExceptionClass(

15、int val) size = val; try checkSize(); catch (ArraySizeException e) System.out.println(e); /* 声明变量以存储数组的大小和元素. */ private int size; private int array; /* 检查数组长度的方法. * throws 一个 ArraySizeException */ public void checkSize() throws ArraySizeException if (size 0) throw new ArraySizeException(); array =

16、new int3; for (int count = 0; count 3; count+) arraycount = count + 1; class UserDefinedExceptions /* 构造方法. */ protected UserDefinedExceptions() /* * 类和应用程序的唯一入口点. * param arg 字符串参数的数组 */ public static void main(String arg) Exceptio Class bj = new ExceptionClass(Integer.parseInt(arg0); 总结q异常是运行时发生的错误q可以使用 try、catch、throw、throws 和 finally 来管理 Java 异常处理。要监控的程序语句包含在 try 块内catch 块中的代码用于捕获和处理异常。在方法返回之前绝对必须执行的代码应放置在 finally 块中q要手动引发异常,使用关键字 throw。任何被抛到方法外部的异常都必须用 throws 子句指定q多重catch 和嵌套try-catch的使用q自定义异常的编写和使用

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!