版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第五章异常第五章异常回顾继承及其JAVA实现多态及其JAVA实现访问修饰符对类成员的访问限制方法修饰符:static、final、abstract2回顾继承及其JAVA实现2目标理解异常的概念 运用try块、catch块和finally块处理异常运用多重catch块处理异常运用嵌套try/catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常3目标理解异常的概念 3什么是异常?publicclassExceptionRaised{publicExceptionRaised(){}publicintcalculate(intoperand1,intoperand2){intresult=operand1/operand2;returnresult;}publicstaticvoidmain(String[]args){ExceptionRaisedobj=newExceptionRaised();intresult=obj.calculate(9,0);System.out.println(result);}}OS!异常情况异常程序突然终止并将控制交给操作系统在运行时发生的错误
4什么是异常?publicclassExceptionRa………IFBISZEROGOTOERRORC=A/BPRINTCGOTOEXITERROR:
处理异常的块“以零作除数,代码导致错误”
DISPLAY
EXIT:END处理异常2-1处理运行时错误的伪代码5………处理异常2-1处理运行时错误的伪代码5手动引发异常指定由方法引发的异常
tryfinallycatchthrowsthrow处理异常2-2要监控的程序语句包含在此块中以合理的方式捕获和处理异常释放资源等6手动引发异常指定由方法引发的异常tryfinallyJava异常类文件结束EOFException找不到文件FileNotFoundExceptionI/O异常的根类IOException数字转化格式异常,比如字符串到float型数字的转换无效NumberFormatException不能加载所需的类ClassNotFoundException方法接收到非法参数IllegalArgumentException数组大小小于或大于实际的数组大小ArrayIndexOutOfBoundException尝试访问null对象成员NullPointerException许多java.lang异常的基类RuntimeException异常层次结构的根类Exception算术错误情形,如以零作除数ArithmeticException线程中断InterruptedException说明异常7Java异常类文件结束EOFException找不到文件Ftry和catch块2-1trycatch异常执行catch后程序继续正常运行程序控制引发代码块单元8try和catch块2-1trycatch异常执行try和catch块2-2演示:示例1try和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();}}}9try和catch块2-2演示:示例1try和finally块try块finally块catch块无异常异常try、catch和finally块的执行流程10finally块try块finally块catch块异常处理块的一般形式try{ //要监控错误的代码块methodGeneratingException();}catch(Exceptione){ //Exceptione
的异常处理程序}finally{ //在try结束前要执行的代码块cleanup();}11异常处理块的一般形式try{11多重catch块3-1一段代码可能会生成多个异常当引发异常时,会按顺序来查看每个catch语句,并执行第一个类型与异常类型匹配的语句执行其中的一条catch语句之后,其他的catch语句将被忽略
try{…….}catch(ArrayIndexOutOfBoundsExceptione){……}catch(Exceptione){……}12多重catch块3-1一段代码可能会生成多个异常try{ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeathSQLExceptionRuntimeExceptionNumberFormatException……异常类的层次结构Throwable具有两个子类,它们是Exception:处理用户程序应当捕获的异常情况Error:Error类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们AWTError13ExceptionArithmeticExceptionNu多重catch块3-2使用多重catch语句时,异常子类一定要位于异常父类之前
try{…...}catch(Exceptione){……}catch(ArrayIndexOutOfBoundsExceptione){……}
×14多重catch块3-2使用多重catch语句时,异常多重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();}}15多重catch块3-3演示:示例2多重catch的使用嵌套try–catch块如果内层try没有相应的catch,则检查外层catchclassNestedException{/*构造方法。*/protectedNestedException(){}/**这个方法检测数字的格式。*@paramargument用于存储args的值。*/publictest(String[]argumnet){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]);}}因此需要嵌套异常处理程序!!16嵌套try–catch块如果内层try没有相应使用throw和throws2-1语句3throw异常引发的异常!停止异常处理程序可执行程序语句语句1语句217使用throw和throws2-1语句3thro使用throw和throws2-2处理异常被调用的方法调用方法处理异常可能会导致异常防止被调用的方法出现异常并处理异常typecalledmethod-name(parameter-list)throwsexception-list{//bodyofmethod}typecallingmethod-name{try{//statements calledmethod-name();}catch(Exceptione){//statements}}18使用throw和throws2-2处理异常被调用的方用户自定义异常2-1自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常自定义异常需要继承Exception及其子类19用户自定义异常2-1自定义异常概念19classArraySizeExceptionextendsNegativeArraySizeException{/**构造方法。*/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]));}}20classArraySizeExceptionexten总结异常是运行时发生的错误可以使用try、catch、throw、throws和finally来管理Java异常处理。要监控的程序语句包含在try块内catch块中的代码用于捕获和处理异常。在方法返回之前绝对必须执行的代码应放置在finally块中要手动引发异常,使用关键字throw。任何被抛到方法外部的异常都必须用throws子句指定多重catch和嵌套try-catch的使用自定义异常的编写和使用21总结异常是运行时发生的错误21第五章异常第五章异常回顾继承及其JAVA实现多态及其JAVA实现访问修饰符对类成员的访问限制方法修饰符:static、final、abstract23回顾继承及其JAVA实现2目标理解异常的概念 运用try块、catch块和finally块处理异常运用多重catch块处理异常运用嵌套try/catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常24目标理解异常的概念 3什么是异常?publicclassExceptionRaised{publicExceptionRaised(){}publicintcalculate(intoperand1,intoperand2){intresult=operand1/operand2;returnresult;}publicstaticvoidmain(String[]args){ExceptionRaisedobj=newExceptionRaised();intresult=obj.calculate(9,0);System.out.println(result);}}OS!异常情况异常程序突然终止并将控制交给操作系统在运行时发生的错误
25什么是异常?publicclassExceptionRa………IFBISZEROGOTOERRORC=A/BPRINTCGOTOEXITERROR:
处理异常的块“以零作除数,代码导致错误”
DISPLAY
EXIT:END处理异常2-1处理运行时错误的伪代码26………处理异常2-1处理运行时错误的伪代码5手动引发异常指定由方法引发的异常
tryfinallycatchthrowsthrow处理异常2-2要监控的程序语句包含在此块中以合理的方式捕获和处理异常释放资源等27手动引发异常指定由方法引发的异常tryfinallyJava异常类文件结束EOFException找不到文件FileNotFoundExceptionI/O异常的根类IOException数字转化格式异常,比如字符串到float型数字的转换无效NumberFormatException不能加载所需的类ClassNotFoundException方法接收到非法参数IllegalArgumentException数组大小小于或大于实际的数组大小ArrayIndexOutOfBoundException尝试访问null对象成员NullPointerException许多java.lang异常的基类RuntimeException异常层次结构的根类Exception算术错误情形,如以零作除数ArithmeticException线程中断InterruptedException说明异常28Java异常类文件结束EOFException找不到文件Ftry和catch块2-1trycatch异常执行catch后程序继续正常运行程序控制引发代码块单元29try和catch块2-1trycatch异常执行try和catch块2-2演示:示例1try和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();}}}30try和catch块2-2演示:示例1try和finally块try块finally块catch块无异常异常try、catch和finally块的执行流程31finally块try块finally块catch块异常处理块的一般形式try{ //要监控错误的代码块methodGeneratingException();}catch(Exceptione){ //Exceptione
的异常处理程序}finally{ //在try结束前要执行的代码块cleanup();}32异常处理块的一般形式try{11多重catch块3-1一段代码可能会生成多个异常当引发异常时,会按顺序来查看每个catch语句,并执行第一个类型与异常类型匹配的语句执行其中的一条catch语句之后,其他的catch语句将被忽略
try{…….}catch(ArrayIndexOutOfBoundsExceptione){……}catch(Exceptione){……}33多重catch块3-1一段代码可能会生成多个异常try{ExceptionArithmeticExceptionNullPointerExceptionObjectThrowableErrorThreadDeathSQLExceptionRuntimeExceptionNumberFormatException……异常类的层次结构Throwable具有两个子类,它们是Exception:处理用户程序应当捕获的异常情况Error:Error类的异常为内部错误,因此在正常情况下不期望用户的程序捕获它们AWTError34ExceptionArithmeticExceptionNu多重catch块3-2使用多重catch语句时,异常子类一定要位于异常父类之前
try{…...}catch(Exceptione){……}catch(ArrayIndexOutOfBoundsExceptione){……}
×35多重catch块3-2使用多重catch语句时,异常多重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();}}36多重catch块3-3演示:示例2多重catch的使用嵌套try–catch块如果内层try没有相应的catch,则检查外层catchclassNestedException{/*构造方法。*/protectedNestedException(){}/**这个方法检测数字的格式。*@paramargument用于存储args的值。*/publictest(String[]argumnet){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]);}}因此需要嵌套异常处理程序!!37嵌套try–catch块如果内层try没有相应使用throw和throws2-1语句3throw异常引发的异常!停止异常处理程序可执行程序语句语句1语句238使用throw和throws2-1语句3thro使用throw和throws2-2处理异常被调用的方法调用方法处理异常可能会导致异常防止被调用的方法出现异常并处理异常typecalledmethod-name(parameter-list)throwsexception-list{//bodyofmethod}typecallingmethod-name{try{//statements calledmethod-name();}catch(Exceptione){//statements}}39使用throw和throws2-2处理异常被调用的方用户自定义异常2-1自定义异常概念使用自定义异常的时候JavaAPI提供的内置异常不一定总能捕获程序中发生的所有错误。有时会需要创建用户自定义异常自定义异常需要继承Exceptio
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年福安市水利局招聘事业单位紧缺急需人才笔试参考题库及答案解析
- 2026湖南长银五八社会招聘考试备考试题及答案解析
- 2026年1月广东广州市天河区枫叶幼儿园编外教职工招聘3人考试备考题库及答案解析
- 2026甘肃电投常乐发电有限责任公司招聘30人考试参考题库及答案解析
- 2026内蒙古赤峰市敖汉旗就业服务中心招聘第一批公益性岗位人员166人考试备考试题及答案解析
- 2026年甘肃省武威市古浪县黑松驿镇选聘大学生村文书考试备考题库及答案解析
- 2026辽宁省精神卫生中心招聘高层次和急需紧缺人才7人笔试模拟试题及答案解析
- 2026年海南省国有资本运营有限公司招聘备考题库完整答案详解
- 2026年财达证券股份有限公司天津狮子林大街证券营业部招聘备考题库及1套参考答案详解
- 2026年整合药物研究中心(3100)组群组长招聘备考题库完整参考答案详解
- 道路危险货物运输企业安全隐患排查与治理制度
- 京东物流合同范本
- 养老机构安全生产责任制清单
- 《红岩》中考试题(解析版)-2026年中考语文名著复习核心知识梳理与专项训练
- 非洲鼓基础知识培训课件
- 2026-2031中国酿酒设备行业市场现状调查及投资前景研判报告
- KET考试必背核心短语(按场景分类)
- 2025四川产业振兴基金投资集团有限公司应届毕业生招聘9人笔试历年难易错考点试卷带答案解析2套试卷
- 2025年智能眼镜行业分析报告及未来发展趋势预测
- 精防医生考试试题及答案
- 天然气制氢项目可行性研究报告
评论
0/150
提交评论