已阅读5页,还剩24页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
异常 2 目标 理解异常的概念运用try块 catch块和finally块处理异常运用多重catch块处理异常运用嵌套try catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常 3 异常处理的基本概念 异常处理机制的作用在编程中 错误总是难免会发生的关键在于发生错误之后 能否捕获错误 如何捕获错误 程序能否从错误中恢复异常处理的目的 不是要避免异常 而是在异常发生时 设法使损失降低到最小 4 异常处理的基本概念 错误与异常错误 编译程序时发现的问题 编译时会出现错误或警告 当运行程序时 错误已经不存在异常 编译时没有发现 只有在运行程序的时候 在某种特定的情况下 程序执行出现错误 这时会发生异常 5 什么是异常 publicclassExceptionRaised publicExceptionRaised publicintcalculate intoperand1 intoperand2 intresult operand1 operand2 returnresult publicstaticvoidmain String args ExceptionRaisedobj newExceptionRaised intresult obj calculate 9 0 System out println result 异常情况 异常 程序突然终止并将控制交给操作系统 在运行时发生的错误 6 IFBISZEROGOTOERRORC A BPRINTCGOTOEXITERROR 处理异常的块 以零作除数 代码导致错误 DISPLAYEXIT END 处理异常2 1 处理运行时错误的伪代码 7 手动引发异常 指定由方法引发的异常 try finally catch throws throw 处理异常2 2 8 Java异常类 文件结束 EOFException 找不到文件 FileNotFoundException I O异常的根类 IOException 数字转化格式异常 比如字符串到float型数字的转换无效 NumberFormatException 不能加载所需的类 ClassNotFoundException 方法接收到非法参数 IllegalArgumentException 数组大小小于或大于实际的数组大小 ArrayIndexOutOfBoundException 尝试访问null对象成员 NullPointerException 许多java lang异常的基类 RuntimeException 异常层次结构的根类 Exception 算术错误情形 如以零作除数 ArithmeticException 线程中断 InterruptedException 说明 异常 9 try和catch块2 1 try catch 异常 执行catch后程序继续正常运行 程序控制 引发 代码块 单元 10 try和catch块2 2 演示 示例1 try和catch块的用法 classExceptionRaised 构造方法 publicExceptionRaised 这个方法运行时将会产生一个异常 paramoperand1除法中的分子 paramoperand2除法中的分母 returnint返回除法的结果 publicintcalculate intoperand1 intoperand2 intresult operand1 operand2 returnresult publicclassArithmeticException 构造方法 publicArithmeticException publicstaticvoidmain String args ExceptionRaisedobj newExceptionRaised try 定义变量result以存储结果 intresult obj calculate 9 0 System out println result catch Exceptione System err println 发生异常 e toString e printStackTrace 11 finally块 try块 finally块 catch块 无异常 异常 try catch和finally块的执行流程 12 异常处理块的一般形式 try 要监控错误的代码块methodGeneratingException catch Exceptione Exceptione的异常处理程序 finally 在try结束前要执行的代码块cleanup 13 多重catch块3 1 一段代码可能会生成多个异常当引发异常时 会按顺序来查看每个catch语句 并执行第一个类型与异常类型匹配的语句执行其中的一条catch语句之后 其他的catch语句将被忽略 try catch ArrayIndexOutOfBoundsExceptione catch Exceptione 14 Exception ArithmeticException NullPointerException Object Throwable Error ThreadDeath SQLException RuntimeException NumberFormatException 异常类的层次结构 Throwable具有两个子类 它们是Exception 处理用户程序应当捕获的异常情况Error Error类的异常为内部错误 因此在正常情况下不期望用户的程序捕获它们 AWTError 15 多重catch块3 2 使用多重catch语句时 异常子类一定要位于异常父类之前 try catch Exceptione catch ArrayIndexOutOfBoundsExceptione 16 多重catch块3 3 演示 示例2 多重catch的使用 classExceptionCode 构造方法 protectedExceptionCode 这个方法将将零作除数 publicvoidcalculate try intnum 0 intnum1 42 num catch Exceptione System out println 父类异常catch子句 catch ArithmeticExceptionae 错误 不能到达的代码System out println 这个子类的父类是 exception类 且不能到达 classUnreachableCode 构造方法 protectedUnreachableCode 类和应用程序的唯一进入点 paramargs字符串参数的数组 publicstaticvoidmain String args ExceptionCodeobj newExceptionCode obj calculate 17 嵌套try catch块 如果内层try没有相应的catch 则检查外层catch classNestedException 构造方法 protectedNestedException 这个方法检测数字的格式 paramargument用于存储args的值 publicvoidtest String args try intnum Integer parseInt args 1 嵌套try块 try intnumValue Integer parseInt args 0 System out println args 0 的平方是 numValue numValue catch NumberFormatExceptionnb System out println 不是一个数字 catch ArrayIndexOutOfBoundsExceptionne System out println 请输入数字 main方法 publicstaticvoidmain String args NestedExceptionobj newNestedException obj test args 0 因此需要嵌套异常处理程序 18 使用throw和throws2 1 语句3 throw异常 引发的异常 停止 异常处理程序 可执行程序语句 语句1 语句2 19 使用throw 异常是通过关键字throw抛出 程序可以用throw语句引发明确的异常 如 try if flag 0 thrownewNullPointerException throw语句的操作数一定是Throwable类类型或Throwable子类类型的一个对象 20 使用throws 如果一个方法可能导致一个异常但不处理它 此时要求在方法声明中包含throws子句 通知潜在调用者 如果发生了异常 由调用者处理 一个throws子句列举了一个方法可能引发的所有异常类型 这对于除Error或RuntimeException及它们子类以外类型的所有异常是必要的 21 Throw示例 publicclassThrowDemo publicstaticvoidmain Stringargs try intarrSize args length ThrowDemotd newThrowDemo td check arrSize 1 catch ArrayIndexOutOfBoundsExceptione System out println 数组下标越界 voidcheck intarrSize if arrSize 0 thrownewNegativeArraySizeException 数组大小为负 22 使用throw和throws2 2 处理异常 被调用的方法 调用方法 处理异常 可能会导致异常 防止被调用的方法出现异常并处理异常 typecalledmethod name parameter list throwsexception list bodyofmethod typecallingmethod name try statementscalledmethod name catch Exceptione statements 23 使用throws示例 classThrowsDemo staticvoidthrowOne throwsIllegalAccessException System out println 在throwOne中 thrownewIllegalAccessException 非法访问异常 publicstaticvoidmain Stringargs try throwOne catch IllegalAccessExceptione System out println 捕获 e 在该方法中没有处理异常 只是声明可能引发的异常 在throwOne方法的调用函数中捕获并处理异常 24 用户自定义异常2 1 自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误 有时会需要创建用户自定义异常自定义异常需要继承Exception及其子类 25 classArraySizeExceptionextendsNegativeArraySizeException 构造方法 ArraySizeException super 您传递的数组大小非法 用户自定义异常2 2 示例 示例6 创建用户自定义异常继承Exception或其子类 classExceptionClass ExceptionClass intval size val try checkSize catch ArraySizeExceptione System out println e 声明变量以存储数组的大小和元素 privateintsize privateint array 检查数组长度的方法 throws一个ArraySizeException publicvoidcheckSize throwsArraySizeException if size 0 thrownewArraySizeException array newint 3 for intcount 0 count 3 count array count count 1 classUserDefinedExceptions 构造方法 protectedUserDefinedExceptions 类和应用程序的唯一入口点 paramarg字符串参数的数组 publicstaticvoidmain String arg ExceptionClassobj newExceptionClass Integer parseInt arg 0 26 垃圾收集2 1 垃圾收集是可将分配给对象但不再使用的内存回收或释放的过程Java将自动释放不再使用的内存如果一个对象没有指向它的引用或将其赋值为null 则此对象将适于进行垃圾收集 27 垃圾收集2 2 垃圾收集器将作为优先级低的单独线程运行可通过下列方式关闭应用程序中的垃圾收集java noasyncgc 如果关闭了垃圾收集 程序极有可能会因为内存在某个时刻耗尽而失败 28 使用finalize方法 Java提供了一种与C 语言中的析构器相似的方式 可用于在控制返回操作系统前完成清除过程如果存在finali
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 医院法人委托协议书
- 山大体育管理学模拟试题(四套)与参考答案
- 吕梁学院《运动生理学》教学大纲
- 燃气具装配工操作评优考核试卷含答案
- 生物质燃料值班员安全风险测试考核试卷含答案
- 公司有色金属冶炼质检员标准化技术规程
- 尿素脱蜡装置操作工应急处置技术规程
- 大型养路机械司机安全教育知识考核试卷含答案
- 公司海藻制醇工岗位职业健康及安全技术规程
- 新教材高考化学一轮三讲常见物质的制备教案(2025-2026学年)
- 完整版国企钢结构施工工艺指导手册
- 2025年甘肃省白银市靖远县石门乡人民政府选聘专业化管理村文书考试笔试备考题库及答案解析
- 2025云南山水物业服务有限公司招聘(6人)笔试考试参考试题及答案解析
- 十五五规划建议专题测试及答案二
- 责任胜于能力培训课
- 2025年大学《马克思主义理论-马克思主义中国化研究》考试参考题库及答案解析
- (通讯维修工)理论知识考试题库
- 2025至2030中国大豆浓缩蛋白行业市场深度研究与战略咨询分析报告
- 码头雷电应急预案
- 《对世界的不断探索》教案
- 校园安全立体化防控AI预警平台建设用户需求书
评论
0/150
提交评论