版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年劳动关系协调员(4级)职业鉴定考试题库(含答案)
- 农村冷链冷库冻伤应急演练脚本
- 2026年跨境电商物流集货服务合同协议
- 虹膜恶性肿瘤护理查房
- 年产10万只齿轮智能工段技改项目可行性研究报告模板立项申批备案
- 文娱行业内容审核发布制度
- 基础教育课程设置标准制度
- 制造行业安全生产制度
- 生物生态系统结构与功能考点考试
- (正式版)T∕GDSTD 024-2026 广东省自然资源资产收储整备指南
- 2025年成都市中考语文试题卷(含标准答案及解析)
- 消防应急通信课件
- JG/T 395-2012建筑用膜材料制品
- 私车租给公司合同协议
- GB/T 45298-2025土壤制图1∶25 000~1∶500 000土壤质地、酸碱度、盐渍化图的图式、用色及图例规范
- FOCUS-PDCA改善案例-提高术前手术部位皮肤准备合格率医院品质管理成果汇报
- 2024装配式轻钢轻混结构技术规程
- 《 油菜花开春》4-6岁幼儿园小学少儿美术教育绘画课件创意教程教案
- 2024黑龙江东北林业大学入职专职辅导员岗位招聘17人历年(高频重点提升专题训练)共500题附带答案详解
- JTG-3830-2018公路工程建设项目概算预算编制办法
评论
0/150
提交评论