欢迎来到天天文库
浏览记录
ID:55363845
大小:260.03 KB
页数:21页
时间:2020-05-15
《java学习第五章--异-常.pptx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第五章异常回顾继承及其JAVA实现多态及其JAVA实现访问修饰符对类成员的访问限制方法修饰符:static、final、abstract2目标理解异常的概念运用try块、catch块和finally块处理异常运用多重catch块处理异常运用嵌套try/catch块处理异常运用关键字throw和throws处理异常运用JAVA编写和使用自定义异常3什么是异常?4publicclassExceptionRaised{publicExceptionRaised(){}publicintcalculat
2、e(intoperand1,intoperand2){intresult=operand1/operand2;returnresult;}publicstaticvoidmain(String[]args){ExceptionRaisedobj=newExceptionRaised();intresult=obj.calculate(9,0);System.out.println(result);}}OS!异常情况异常程序突然终止并将控制交给操作系统在运行时发生的错误处理异常2-15………IFB
3、ISZEROGOTOERRORC=A/BPRINTCGOTOEXITERROR:处理异常的块“以零作除数,代码导致错误”DISPLAYEXIT:END处理运行时错误的伪代码处理异常2-26手动引发异常指定由方法引发的异常tryfinallycatchthrowsthrow要监控的程序语句包含在此块中以合理的方式捕获和处理异常释放资源等Java异常类7文件结束EOFException找不到文件FileNotFoundExceptionI/O异常的根类IOException数字转化格式异常,比如字符
4、串到float型数字的转换无效NumberFormatException不能加载所需的类ClassNotFoundException方法接收到非法参数IllegalArgumentException数组大小小于或大于实际的数组大小ArrayIndexOutOfBoundException尝试访问null对象成员NullPointerException许多java.lang异常的基类RuntimeException异常层次结构的根类Exception算术错误情形,如以零作除数ArithmeticE
5、xception线程中断InterruptedException说明异常try和catch块2-18trycatch异常执行catch后程序继续正常运行程序控制引发代码块单元try和catch块2-29演示:示例1try和catch块的用法classExceptionRaised{/**构造方法.*/publicExceptionRaised(){}/***这个方法运行时将会产生一个异常.*@paramoperand1除法中的分子*@paramoperand2除法中的分母*@returnint返
6、回除法的结果*/publicintcalculate(intoperand1,intoperand2){intresult=operand1/operand2;returnresult;}}publicclassArithmeticException{/**构造方法.*/publicArithmeticException(){}publicstaticvoidmain(String[]args){ExceptionRaisedobj=newExceptionRaised();try{/*定义变量
7、result以存储结果.*/intresult=obj.calculate(9,0);System.out.println(result);}catch(Exceptione){System.err.println(“发生异常:"+e.toString());e.printStackTrace();}}}finally块10try块finally块catch块无异常异常try、catch和finally块的执行流程异常处理块的一般形式try{//要监控错误的代码块methodGenerating
8、Exception();}catch(Exceptione){//Exceptione的异常处理程序}finally{//在try结束前要执行的代码块cleanup();}11多重catch块3-1一段代码可能会生成多个异常当引发异常时,会按顺序来查看每个catch语句,并执行第一个类型与异常类型匹配的语句执行其中的一条catch语句之后,其他的catch语句将被忽略12try{…….}catch(ArrayIndexOutOfBoundsExceptione){……}catch(Excepti
此文档下载收益归作者所有