




已阅读5页,还剩20页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第10章异常处理 10 1异常处理概述 1 异常 异常是运行时产生的错误 由大量例外情况产生的 如输入错误 不能打开文件 数组下标越界等 程序应该具有一定的容错功能 也就是说 在环境条件出现异常情况下 不会轻易出现死机和灾难性的后果 而应有正确合理的表现 2 异常示例 例10 1 异常示例publicclassTest staticvoidf inta intb System out println a b a b publicstaticvoidmain Stringargs f 10 3 f 5 0 f 3 2 运行结果 10 3 3Exceptioninthread main java lang ArithmeticException byzero 3 传统的异常处理方法 if 是否异常 else if 是否异常 else 例10 2 传统的异常处理方法publicclassTest staticvoidf inta intb if b 0 System out println 除数为0 elseSystem out println a b a b publicstaticvoidmain Stringargs f 10 3 f 5 0 f 3 2 运行结果 10 3 3除数为03 2 1 10 2Java异常处理机制 1 异常处理语句 try 可能产生异常的语句 catch 异常类型异常参数名 异常处理语句 catch 异常类型异常参数名 异常处理语句 finally 无论有无异常都要的处理 好处 将处理异常的代码与程序代码的主线分离 增强程序的可读性 异常按类型区分 不同的异常提供不同的catch语句块异常类型异常参数名 异常匹配顺序 从上到下 按顺序匹配将抛出的异常对象与catch块的异常类型做比较 找到对应的catch块 例10 3 异常处理语句示例publicclassTest staticvoidf inta intb try System out println a b a b catch ArithmeticExceptione System out println 异常处理 除数为0 publicstaticvoidmain Stringargs f 10 3 f 5 0 f 3 2 运行结果 10 3 3异常处理 除数为03 2 1 处理多种类型异常 importjava io publicclassTest publicstaticvoidmain Stringargs System out println 程序开始 try System out println 进入try语句块 BufferedReaderbr newBufferedReader newInputStreamReader System in System out println 输入第一个整数 Stringstr br readLine inti Integer parseInt str System out println 请输入第二个整数 str br readLine intd Integer parseInt str System out println i d System out println 正常退出try语句块 catch IOExceptione System out println IO错误 e toString catch NumberFormatExceptione System out println 数据转换错误 e toString catch ArithmeticExceptione System out println 运算错误 e toString finally System out println finally语句执行 System out println 异常语句以外的语句执行 例10 5 输入3组整数 并输出这3组整数的商 若是除数为0 或输入的不是整数则提示输入错误 并要求重新输入一组数据 importjava io publicclassTest publicstaticvoidmain Stringargs System out println 程序开始 for intn 0 n 3 n try BufferedReaderbr newBufferedReader newInputStreamReader System in System out println 输入第 n 组整数 Stringstr br readLine inti Integer parseInt str str br readLine intd Integer parseInt str System out println 结果为 i d catch IOExceptione System out println IO错误 e toString catch ArithmeticExceptione System out println 除数为0 请重新输入 n catch NumberFormatExceptione System out println 数据格式错误 请重新输入 n System out println 程序结束 10 3Java异常类型 一些预定义的异常类型ArithmeticExceptionNullPointerExceptionNegativeArraySizeExceptionArrayIndexOutOfBoundsExceptionIOExceptionFileNotFoundExceptionSecurityException 类 10 3 1异常类型的继承关系 Java中异常的类结构 Error 很难恢复的错误Exception RuntimeException 一般表示设计或实现方面的问题 可以不处理 ArithmeticExceptionNullPointerExceptionNegativeArraySizeExceptionArrayIndexOutOfBoundsExceptionSecurityException 其他异常 运行时因环境的影响可能发生并可被处理的问题 用户的错误很可能导致这类问题的发生 必须处理 IOExceptionFileNotFoundException 10 3 2 异常类 Exception 的常用方法 Exception类有自己的数据成员和成员方法 其构造方法为 publicException publicException Strings 其公有的方法为 这三个方法都是从Throwable类继承而来 publicStringgetMessage 返回详细的异常信息publicStringtoString 返回对异常类型的简要说明publicvoidprintStackTrace 输出调用堆栈路径 10 3 3 异常的继承关系的探讨 注意 由于异常之间的继承关系 基于父类的异常处理可以捕获其所有的子类异常对象 例10 6 父类异常捕获子类对象importjava io publicclassTest publicstaticvoidmain Stringargs try System out println 输入两个整数 BufferedReaderbr newBufferedReader newInputStreamReader System in Stringstr br readLine inti Integer parseInt str str br readLine intd Integer parseInt str System out println i d catch Exceptione System out println e toString 引申出这样的结论 1 若有多个catch子语句块处理多种异常类型 那么对父类异常的处理应在子类异常的后面 2 Exception类可以捕获任何类型异常 因此无论何时 其对应的catch子语句块应该放在所有其他catch子语句块的末尾 10 4方法声明抛出异常throws 程序员可以不在当前方法内处理异常 而是把异常抛出到调用方法中 方法头声明抛出异常 运行时异常 RuntimeException类及其子类 可以不被处理 同样 运行时异常若不处理 也可以不用throws声明 但其他异常若在方法内不被处理 则必须用throws声明 修饰符 返回类型方法名 参数表 throws异常类型1 异常类型2 会产生异常类型1异常的语句 会产生异常类型2异常的语句 调用声明抛出异常的方法时 需要进行异常处理 或者也将异常抛出 由其上级调用者处理 异常的传播过程发现异常的方法若不能处理异常 这时异常被抛出给其上级调用者 如果调用者也不能处理这个异常 还可以继续传递给上级调用者去处理 这种传播会一直继续到异常被处理为止 如果程序始终没处理这个异常 异常会传递到Java虚拟机运行时系统 运行时系统捕获这异常后通常只是简单地终止这个程序 例10 7 throws示例publicclassTest staticvoidf inta intb throwsArithmeticException System out println a b a b publicstaticvoidmain Stringargs try f 10 3 f 5 0 f 3 2 catch ArithmeticExceptione System out println e toString 例10 8 输入一个整数 如果输入错误提示重新输入 直到输入正确为止 importjava io publicclassTest publicintinput throwsIOException NumberFormatException BufferedReaderbr newBufferedReader newInputStreamReader System in Stringstr br readLine inti Integer parseInt str returni publicstaticvoidmain Stringargs inti 0 booleanflag false while flag false try Testa newTest i a input flag true catch IOExceptione catch NumberFormatExceptione System out println 请输入整数 System out println i i 10 5抛出自定义异常throw 自定义异常 从Exception继承的类异常类中可以有与异常相关的属性和方法 可以被异常处理调用在方法内抛出自定义异常方法体内 创建异常对象 并抛出 throw异常对象 方法头 声明抛出异常列表 classMyExceptionextendsException privateintdetail MyException inti detail i publicStringtoString return MyException detail 自定义异常 从Exception继承的类 求负数的阶乘的异常 classFactorial staticdoublecompute inta throwsMyException if a 0 thrownewMyException a else doubles 1 0 for inti 1 i a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025云南省普洱市景谷县政务服务中心综合窗口招聘工作人员(5人)笔试参考题库附答案解析
- 2025福建省医药有限责任公司招聘1人笔试参考题库附答案解析
- 2025广东湛江吴川市公益性岗位人员招聘3人笔试模拟试题及答案解析
- 2025四川甘孜州色达县招募医疗卫生辅助岗工作人员3人考试模拟试题及答案解析
- 中文系毕业论文分数查询
- 2025广西玉林市公安局玉州分局第一次公开招聘辅警15人笔试模拟试题及答案解析
- 2025云南广南民族文化旅游产业有限公司招聘14人笔试备考题库及答案解析
- 毕业论文中期检查
- 本科毕业论文写作技巧
- 2025上海对外经贸大学金融管理学院教学秘书招聘考试模拟试题及答案解析
- 土地管理培训课件
- 2025版新《中华人民共和国治安管理处罚法》全文课件(原创)
- DB32∕T 4981-2024 公路水运工程平安工地建设规范
- 2025年山西中考历史试卷真题解读及答案讲解课件
- 2025至2030中国科技成果转换行业发展趋势分析与未来投资战略咨询研究报告
- 除颤仪使用讲课件
- 中国PCBA行业发展前景及发展策略与投资风险研究报告2025-2028版
- 中国产科麻醉专家共识
- 2025-2030年中国牙缺失治疗行业市场现状供需分析及投资评估规划分析研究报告
- 教育科技公司团队管理制度
- 特殊人群服务管理制度
评论
0/150
提交评论