第17章 异常和断言.ppt_第1页
第17章 异常和断言.ppt_第2页
第17章 异常和断言.ppt_第3页
第17章 异常和断言.ppt_第4页
第17章 异常和断言.ppt_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第17章异常 17 1引言17 2异常处理概述17 3异常和异常类型17 4理解异常处理17 5finally子句17 6何时使用异常17 7重新抛出异常 自学 17 8链式异常 可选 17 9创建自定义异常类 可选 17 10断言 可选 17 1引言 错误类型语法错误 syntaxerrors 没有遵循语言规则运行错误 Runtimeerrors 发生一个不可能执行的操作逻辑错误 logicerrors 没有按照预期的方案执行异常 处理运行错误 17 2异常处理概述 常见异常 用户输入无效值 程序打开不存在的文件 网络中断 数组下标越界等异常处理 当产生一个异常 正常的程序执行流程就会中断 异常处理提供给程序员处理运行刑场的功能常见异常处理类ArrayIndexOutOfBOundsExceptionNullPointerExceptionStringIndexOutOfBoundsException ExceptionDemo java 当用户输入3 5 修改程序 HandleExceptionDemo java 17 3异常和异常类型 Throwable是所有异常类的父类 在java lang中 子类可以在不同的包中异常类三种类型 系统错误 Error 异常 Exception 以及运行异常 RuntimeException 17 3异常和异常类型 系统错误 Systemerrors 由java虚拟机抛出并在Error类中表述 Error类描述内部的系统错误 这种错误很少发生 如果发生 除了通知用户以及尽量妥善的结束程序 几乎什么都不能做 17 3异常和异常类型 异常 Exception 描述程序和外部环境引起的错误 这些错误能通过程序捕获和处理 17 3异常和异常类型 运行异常 RuntimeException 由RuntimeException描述 经常描述编程错误 比如不合适的转换 访问一个越界数组或数值错误等 17 3异常和异常类型 异常分类系统错误 Error 描述系统内部的错误异常 Exception 描述程序和外部环境的错误运行异常 RuntimeException 描述编程错误免检异常 UncheckedException RuntimeException Error以及他们的子类 java不允许程序员编写捕获或声明免检异常的代码必检异常 CheckedExceptions 除RuntimeException Error以及他们的子类外的所有异常 编译器要求程序员检查并处理他们 17 4理解异常错误 17 4 1声明异常17 4 2抛出异常17 4 3捕获异常 17 4 1声明异常 定义 每个方法都必须说明它可能抛出的必检异常的类型形式 publicvoidMethord throwsException1 Exception2 ExceptionN 17 4 2抛出异常 定义 程序检查到一个错误后 创建一个适当类型异常的实例并抛出它形式 thrownewTheException TheExceptionex newTheException throwex Setanewradius publicvoidsetRadius doublenewRadius throwsIllegalArgumentException if newRadius 0 radius newRadius elsethrownewIllegalArgumentException Radiuscannotbenegative 17 4 3捕获异常 异常处理器 ExceptionHandler 处理异常的代码定义 寻找处理器的整个过程称为捕获异常 try statements Statementsthatmaythrowexceptions catch Exception1exVar1 handlerforexception1 catch Exception2exVar2 handlerforexception2 catch ExceptionNexVar3 handlerforexceptionN 17 4 3捕获异常 实例 图17 5如果异常类型是Exception3 methord3终止 控制返回methord2 跳过statement5 执行statement6如果异常类型是Exception2 methord2终止 控制返回methord1 跳过statement3 执行statement4如果异常类型是Exception1 methord1终止 控制返回main 跳过statement1 执行statment2如果异常类型不是Exception3 Exception2 Exception1 程序终止 17 5finally子句 功能 finally子句中的部分不管异常是否出现或者被捕获都会被执行形式 调用的3种情况用途 通常用于I O编程实例 17 6FinallyDemo java 自学 try statements catch TheExceptionex handlingex finally finalStatements 17 5finally子句 三种情况之一 try statements catch TheExceptionex handlingex finally finalStatements Nextstatement 假设没有异常发生 17 5finally子句 三种情况之一 try statements catch TheExceptionex handlingex finally finalStatements Nextstatement Finally语句块总被执行 17 5finally子句 三种情况之一 try statements catch TheExceptionex handlingex finally finalStatements Nextstatement 方法中的nextStatement被执行 17 5finally子句 三种情况之二 try statement1 statement2 statement3 catch Exception1ex handlingex finally finalStatements Nextstatement 假设statement2语句发生Exception1异常 17 5finally子句 三种情况之二 try statement1 statement2 statement3 catch Exception1ex handlingex finally finalStatements Nextstatement Exception1异常被处理 17 5finally子句 三种情况之二 try statement1 statement2 statement3 catch Exception1ex handlingex finally finalStatements Nextstatement 然后执行finally语句块 17 5finally子句 三种情况之二 try statement1 statement2 statement3 catch Exception1ex handlingex finally finalStatements Nextstatement 最后执行nextstatement语句块 17 5finally子句 三种情况之三 try statement1 statement2 statement3 catch Exception1ex handlingex catch Exception2ex handlingex throwex finally finalStatements Nextstatement 假设statement2抛出Exception2异常 17 5finally子句 三种情况之三 try statement1 statement2 statement3 catch Exception1ex handlingex catch Exception2ex handlingex throwex finally finalStatements Nextstatement 处理异常 17 5finally子句 三种情况之三 try statement1 statement2 statement3 catch Exception1ex handlingex catch Exception2ex handlingex throwex finally finalStatements Nextstatement 执行finally语句块 17 5finally子句 三种情况之三 try statement1 statement2 statement3 catch Exception1ex handlingex catch Exception2ex ha

温馨提示

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

评论

0/150

提交评论