版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 48349.4-2026信息技术数字视网膜系统第4部分:边子系统
- T/SXJP 066-2023冷鲜猪肉
- T/SZBX 106-2022农家乐餐饮服务规范
- T/EPIAJL 3-2019 T/LEPA 1-2019超期服役火电机组寿命评估技术细则
- T/CI 1259-2025填料硫酸钙 (MR硫酸钙)
- T/CAAM 0004-2025频谱热疗房
- T/COEMA 25-2026激光安全员岗位规范
- 2025~2026学年河南省安阳市滑县人教版六年级上册12月月考数学试卷【附答案】
- 2025~2026学年安徽合肥市庐江县苏教版第二册四年级数册末质量检测试卷(A卷)【附答案】
- 2025-2026学年湖北省宜都市初中历史九年级期末下册通关重点试题(附答案)
- 《论语》十二章考点复习资料汇编
- 400个小学英语高频固定搭配大全
- 《允许一切发生》读书感悟
- 邮轮餐饮服务双语实训指导-西餐宴会服务常见问题
- 学堂在线 批判性思维-方法和实践 章节测试答案
- (2025年标准)更换玻璃维修协议书
- 村级水管员业务知识培训课件
- 文明丧葬规则解读
- 电机学教案本
- 跳高技术动作要领
- 奖励旅游市场42课件
评论
0/150
提交评论