![异常的捕获与处理_第1页](http://file1.renrendoc.com/fileroot_temp2/2020-12/2/0aa5fe22-e972-4306-b2b0-7723319ee57c/0aa5fe22-e972-4306-b2b0-7723319ee57c1.gif)
![异常的捕获与处理_第2页](http://file1.renrendoc.com/fileroot_temp2/2020-12/2/0aa5fe22-e972-4306-b2b0-7723319ee57c/0aa5fe22-e972-4306-b2b0-7723319ee57c2.gif)
![异常的捕获与处理_第3页](http://file1.renrendoc.com/fileroot_temp2/2020-12/2/0aa5fe22-e972-4306-b2b0-7723319ee57c/0aa5fe22-e972-4306-b2b0-7723319ee57c3.gif)
![异常的捕获与处理_第4页](http://file1.renrendoc.com/fileroot_temp2/2020-12/2/0aa5fe22-e972-4306-b2b0-7723319ee57c/0aa5fe22-e972-4306-b2b0-7723319ee57c4.gif)
![异常的捕获与处理_第5页](http://file1.renrendoc.com/fileroot_temp2/2020-12/2/0aa5fe22-e972-4306-b2b0-7723319ee57c/0aa5fe22-e972-4306-b2b0-7723319ee57c5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、20. 异常的捕获与处理 本季讲解了Java中的异常处理程序,讲解了异常的捕获与处理的基本过 程,throw与throws关键字、自定义异常操作类等。 blog: 零基础学JAVAJava SE面向对象部分-20.异常的捕获与处理 2009-02-11异常的概念指程序中断执行的一种指令流publicclassDemo01publicstaticvoidmain(String args)inti = 0;intj = 10;System.out.println(程序代码执行之前);System.out.println(i/j);System.out.println(程序代码执行之后);现在我们反
2、过来哈,j/ipublicclassDemo01publicstaticvoidmain(String args)inti = 0;intj = 10;System.out.println(程序代码执行之前);System.out.println(j/i);System.out.println(程序代码执行之后);异常一旦发生,则在异常发生处之后的所有代码都不再被执行了。异常的分类只要一发生了异常,则肯定会抛出一个异常类的实例化对象。面试问题:RuntimeException与 Exception的区别: RuntimeException表示异常可以由JVM进行处理 Exception:表示用
3、户可以自行处理 实际上两个异常并没有太多的区别,用户都是可以处理的。 Error:不能由用户自行处理的异常。捕获异常(1)publicclassDemo02publicstaticvoidmain(String args)inti = 0;intj = 10;System.out.println(程序代码执行之前);try/下面跟上可能发生错误的代码段System.out.println(j/i);System.out.println(-);catch(ArithmeticException ae)/进行异常处理System.out.println(运算发生了异常);System.out.pr
4、intln(程序代码执行之后);加入异常处理之后,程序可以正常的执行完,也可以正常的捕获错误了,但是在try语句中,在发生异常的地方就进行跳转了,所以异常发生语句之后的代码不会被执行。如果程序中有多个异常要处理呢?注意:一个新的操作:将字符串的内容变为数字。publicclassDemo03publicstaticvoidmain(String args)String str =123;/将字符串变为数字inti = Integer.parseInt(str);System.out.println(i*2);在此代码之中,要求字符串的内容必须是由数字组成的。如果不全是数字,我们来看下效果pub
5、licclassDemo03publicstaticvoidmain(String args)String str =123a;/将字符串变为数字inti = Integer.parseInt(str);System.out.println(i*2);要求: 要求可以在JAVA运行程序的后面加入运行时参数,参数为两个数相除的内容。publicclassDemo04publicstaticvoidmain(String args)/从运行时处接收参数inti = Integer.parseInt(args0);intj = Integer.parseInt(args1);System.out.p
6、rintln(程序代码执行之前);try/下面跟上可能发生错误的代码段System.out.println(i/j);System.out.println(-);catch(ArithmeticException ae)/进行异常处理System.out.println(运算发生了异常);System.out.println(程序代码执行之后);在以上的程序之中可能存在那些问题呢? 如果没有输入参数的时候,则会有错误:ArrayIndexOutOfBoundsException(数组下标越界) 如果输入的参数的内容不是数字,则会有错误:NumberFormatException(数字格式化异常
7、) 如果输入的被除数为零,则会有错误:ArithmeticException(算术异常)则此程序代码只有一个catch肯定是不够的,需要处理多个异常。publicclassDemo05publicstaticvoidmain(String args)/从运行时处接收参数inti = 0;intj = 0;System.out.println(程序代码执行之前);tryi=Integer.parseInt(args0);j=Integer.parseInt(args1);/下面跟上可能发生错误的代码段System.out.println(i/j);System.out.println(-);ca
8、tch(ArithmeticException ae)/进行异常处理System.out.println(ae);System.out.println(1.运算发生了异常);catch(NumberFormatException ne)System.out.println(ne);System.out.println(2.输入的内容不是数字);catch(ArrayIndexOutOfBoundsException ae)System.out.println(ae);System.out.println(3.输入的参数个数不正确);System.out.println(程序代码执行之后);以上
9、的程序只要是有异常发生了,则会自动找到对应的catch语句,分别进行处理,但是一个程序可能会存在各种问题,那么有可能全部的catch都写出来吗?ArithmeticException是Exception的子类哈publicclassDemo06publicstaticvoidmain(String args)/从运行时处接收参数inti = 0;intj = 0;System.out.println(程序代码执行之前);tryi=Integer.parseInt(args0);j=Integer.parseInt(args1);/下面跟上可能发生错误的代码段System.out.println
10、(i/j);System.out.println(-);catch(ArithmeticException ae)/进行异常处理System.out.println(ae);System.out.println(1.运算发生了异常);catch(NumberFormatException ne)System.out.println(ne);System.out.println(2.输入的内容不是数字);catch(ArrayIndexOutOfBoundsException ae)System.out.println(ae);System.out.println(3.输入的参数个数不正确);c
11、atch(Exception e)System.out.println(异常处理);System.out.println(程序代码执行之后);以上代码需要注意: 在异常处理中,对于catch语句要求处理更粗的异常要放在处理更细的异常之后。我们验证下效果哈publicclassDemo07publicstaticvoidmain(String args)/从运行时处接收参数inti = 0;intj = 0;System.out.println(程序代码执行之前);tryi=Integer.parseInt(args0);j=Integer.parseInt(args1);/下面跟上可能发生错误
12、的代码段System.out.println(i/j);System.out.println(-);catch(Exception e)System.out.println(异常处理);catch(ArithmeticException ae)/进行异常处理System.out.println(ae);System.out.println(1.运算发生了异常);catch(NumberFormatException ne)System.out.println(ne);System.out.println(2.输入的内容不是数字);catch(ArrayIndexOutOfBoundsExcep
13、tion ae)System.out.println(ae);System.out.println(3.输入的参数个数不正确);System.out.println(程序代码执行之后);publicclassDemo08publicstaticvoidmain(String args)/从运行时处接收参数inti = 0;intj = 0;System.out.println(程序代码执行之前);tryi=Integer.parseInt(args0);j=Integer.parseInt(args1);/下面跟上可能发生错误的代码段System.out.println(i/j);System
14、.out.println(-);/只要是程序运行时产生的异常肯定可以使用Exception进行接收catch(Exception e)System.out.println(异常处理);System.out.println(程序代码执行之后);捕获异常(2)不管是否发生了异常,都要执行finally代码。classMathpublicintdiv(inti,intj)returni/j;publicclassDemo10publicstaticvoidmain(String args)System.out.println(newMath().div(1,1);这个代码现在没问题哈classMat
15、h/表示此方法必须被处理异常,必须在调用处处理publicintdiv(inti,intj)throwsExceptionreturni/j;publicclassDemo10publicstaticvoidmain(String args)System.out.println(newMath().div(1,1);我们必须要对此代码进行catch捕获classMath/表示此方法必须被处理异常,必须在调用处处理publicintdiv(inti,intj)throwsExceptionreturni/j;publicclassDemo10publicstaticvoidmain(String
16、 args)trySystem.out.println(newMath().div(1,1);catch(Exception e)System.out.println(e);正常执行输出哈classMath/表示此方法必须被处理异常,必须在调用处处理publicintdiv(inti,intj)throwsExceptionreturni/j;publicclassDemo10publicstaticvoidmain(String args)trySystem.out.println(newMath().div(1,0);catch(Exception e)System.out.println
17、(e);出现异常时成功捕获异常同样道理,既然可以在方法上声明,则肯定也可以在main方法处声明。classMath/表示此方法必须被处理异常,必须在调用处处理publicintdiv(inti,intj)throwsExceptionreturni/j;publicclassDemo11/此处的错误交给JVM进行处理了publicstaticvoidmain(String args)throwsExceptionSystem.out.println(newMath().div(1,0);publicclassDemo12publicstaticvoidmain(String args)thro
18、wnewException(自己抛出的异常);提示要进行try.catch的处理哈publicclassDemo12publicstaticvoidmain(String args)trythrownewException(自己抛出的异常);catch(Exception e)System.out.println(e);这就是throw关键字的作用哈实际上在开发中:finally、throw、throws都是一起联合使用的。要求: 定义一个除法操作,要求进入方法之前必须有开始进行除法操作的提示,操作完之后要求有除法操作完成的提示。classMathpublicintdiv(inti,intj)
19、System.out.println(开始除法操作);inttemp=0;temp=i/j;System.out.println(开始除法操作);returntemp;publicclassDemo13publicstaticvoidmain(String args)System.out.println(newMath().div(1,1);classMathpublicintdiv(inti,intj)System.out.println(开始除法操作);inttemp=0;temp=i/j;System.out.println(开始除法操作);returntemp;publicclassD
20、emo13publicstaticvoidmain(String args)System.out.println(newMath().div(1,0);这个就出现异常了哈,结束语句也就不出来了我们要将异常交给调用处处理哈classMath/所有的异常肯定交给调用处处理publicintdiv(inti,intj)throwsExceptionSystem.out.println(开始除法操作);inttemp=0;temp=i/j;System.out.println(结束除法操作);returntemp;publicclassDemo13publicstaticvoidmain(String args)trySystem.out.println(newMath().div(1,0);catch(Exception e)System.out.println(e);classMath/所有的异常肯定交给调用处处理publicintdiv(inti,intj)throwsExceptionSystem.out.println(开始除法操作);inttemp=0;/所有的异常不能自己处理
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 古建筑修缮服务合同
- 南京市药店装修合同
- 农业科技成果物流合同
- 商业用地招商合同
- 装修怎么做防水施工方案
- 私人模块化厕所施工方案
- 土地托管协议合同范例
- 土地果树补偿合同范例
- 仓储叉车出租服务合同范例
- 一手合同范本
- DCMM练习题练习试题
- 《工业化建筑施工阶段碳排放计算标准》
- 地下停车场充电桩技术方案建议书
- 幼儿园设施设备安全教育
- 废旧保温棉处置合同范例
- 2024年数据编织价值评估指南白皮书-Aloudata
- 四川省算力基础设施高质量发展行动方案(2024-2027年)
- 托育园老师培训
- 人教版八年级英语上册Unit1-10完形填空阅读理解专项训练
- 脊柱外科护理进修心得
- 4.1中国特色社会主义进入新时代+课件-2024-2025学年高中政治统编版必修一中国特色社会主义
评论
0/150
提交评论