版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
5异常第章本章目标理解异常的概念运用Java实现的异常处理
掌握Java中异常的实现形式掌握Java中实现自定义异常异常
异常就是在程序的运行过程中所发生的异常事件,它中断指令的正常执行。publicclassExceptionDemo{publicstaticvoidmain(String[]args){
intnum1=0,num2=0;
intresult=0;
num1=Integer.parseInt(args[0]); num2=Integer.parseInt(args[1]);
result=num1/num2;
System.out.println(num1+
"/
"+num2+
"="+result);}}当num2为0值程序运行出现异常
演示使用Java出现异常异常演示异常处理的方法
IFBISZEROGOTOERROR
C=A/BPRINTCGOTOEXITERROR:异常处理块DISPLAYEXIT:END超市异常处理的方法
保安室监控保安小偷Java的异常类
Java的异常类
异常说明Exception异常层次结构的根类RuntimeException许多java.lang异常的基类ArithmeticException算术错误情形,如以零作除数IllegalArgumentException方法接收到非法参数ArrayIndexOutOfBoundsException数组大小小于或大于实际的数组大小NullPointerException尝试访问null对象成员ClassNotFoundException不能加载所需的类NumberFormatException数字转化格式异常,如字符串转换浮点IOExceptionI/O异常的根类FileNotFoundException找不到文件EOFException文件结束InterruptedException线程中断try-catch块
try{result=num1/num2;
System.out.println("计算结果为"
+result);}catch(Exceptione){System.out.println(e.toString());
e.printStackTrace();
}演示使用try-catch块实现异常处理try-catch块异常处理演示try-catch-finally块
try{result=num1/num2;
System.out.println("计算结果"+result);}catch(Exceptione){System.out.println(e.toString());
e.printStackTrace();
}finally{
System.out.println("此块中释放资源");
}演示使用try-catch-finally块实现异常处理try-catch-finally块异常处理演示多重catch块
try块catch块catch块catch块异常类由子类到父类...try{ num1=Integer.parseInt(args[0]);num2=Integer.parseInt(args[1]);
System.out.println("开始计算");result=num1/num2;
System.out.println("计算结果"+result);}catch(ArrayIndexOutOfBoundsExceptione){
System.out.println("参数传入不足两个");
e.printStackTrace();} catch(NumberFormatExceptione){
System.out.println("参数不是数字");
e.printStackTrace();}catch(Exceptione){e.printStackTrace();}演示使用多重catch块实现异常处理多重catch块异常处理演示嵌套try-catch块
try{ num1=Integer.parseInt(args[0]);
try{ num2=Integer.parseInt(args[1]); result=num1/num2;
System.out.println("计算结果"+result);}
catch(ArithmeticExceptione){
System.out.println("除数不能为零");}}catch(ArrayIndexOutOfBoundsExceptione){
System.out.println("参数传入不足两个");} catch(NumberFormatExceptione){
System.out.println("参数不是数字");}catch(Exceptione){e.printStackTrace();}嵌套
try-catch块外部
try-catch块演示使用嵌套catch块实现异常处理嵌套catch块异常处理演示throw语句
publicclassThrowDemo{
public
staticvoidmain(String[]args){……num1=Integer.parseInt(args[0]);num2=Integer.parseInt(args[1]); System.out.println("开始计算");try{
if(num2==0)
thrownewArithmeticException();result=num1/num2;
System.out.println("计算完毕:结果为"+result);}catch(Exceptione){e.printStackTrace();}……}}ArithmeticException异常抛出异常捕获并处理异常发现异常通知catch块throws语句
classThrowTest{
voidcompute(String[]args)throwsException
{
intnum1=0,num2=0;
intresult=0; num1=Integer.parseInt(args[0]);num2=Integer.parseInt(args[1]);
System.out.println("开始计算");
if(num2==0)
thrownewException();result=num1/num2;
System.out.println("计算完毕:结果为"+result);
System.out.println("计算过程结束");}}抛出异常向上抛出异常throws语句
publicclassThrowDemo{
publicstaticvoidmain(String[]args){ThrowTesttt=newThrowTest();
try
{ pute(args);}
catch(Exceptione){e.printStackTrace();}
System.out.println("运行结束");}}创建对象-compute方法可以抛出异常当参数arg[1]为0compute方法抛出异常发现异常通知catch块捕获并处理异常演示使用throws实现从异常抛出异常Throws语句演示自定义异常
classUserExceptionDemoextends
ArithmeticException{UserExceptionDemo(){super("除数为零");}}第一步:创建异常类创建自定义异常类自定义异常
classUseUserException{
voidcompute(String[]args)throwsUserExceptionDemo{ ……
num2=Integer.parseInt(args[1]);
if(num2==0)
thrownewUserExceptionDemo();result=num1/num2;
System.out.println("计算完毕:结果为"+result);}UseUserException(String[]args){ try{ compute(args); } catch(UserExceptionDemoe){ e.printStackTrace(); }}}第二步:使用异常类抛出自定义异常参数args[1]=0抛出自定义异常自定义异常
publicclassUserException{
publicstaticvoidmain(Stringargs[]){UseUserExceptionuue=newUseUserException(args);
System.out.println("程序运行结束");}}第三步:测试异常类创建捕获自定义异常的对象演
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学校长教学领导实践与启示:多案例深度剖析
- 小孔径载体驱动的低成本陶瓷膜制备技术与多元应用探究
- 2026年高考英语模拟试卷必刷题-阅读表达
- 2025年上海大学马克思主义基本原理概论期末考试参考题库
- 2026年电商运营面试题库与答题策略
- 2026届黑龙江龙东十校联盟高三上学期12月考语文试题含答案
- 湘潭科技职业学院《形势与政策》2023-2024学年第一学期期末试卷
- 护理职业伦理与医疗差错防范
- 气管镜护理中的多学科合作模式
- 公职人员违规借贷问题专项治理工作排查报告
- 学堂在线 雨课堂 学堂云 批判性思维-方法和实践 章节测试答案
- 2025年国家开放大学《公共经济学》期末考试备考试题及答案解析
- 修船合同范本
- AQ 1119-2023 煤矿井下人员定位系统技术条件
- 三级公立医院绩效考核微创手术目录(2022版)
- 第六单元 第4课时《解决问题-之间有几人》教学设计 人教版一年级数学上册
- 香港验血测性别报告单
- 研学实践承办机构服务与管理规范
- 车间装置与设备布置的安全分析
- 个人借款借条电子版篇
- 情绪的作文400字五篇
评论
0/150
提交评论