实验五 异常处理
更新时间:2024-03-17 14:34:01 阅读量: 综合文库 文档下载
- 实验五中推荐度:
- 相关推荐
实验五异常处理
一、实验目的
1. 学会利用Try-catch-finally语句来捕获和处理异常; 2. 掌握自定义异常类的方法。
二、实验要求
1. 通过编程理解系统异常处理的机制和创建自定义异常的方法。 2. 掌握基础类。
三、实验内容
(一)异常处理机制
1. 编写使用 try…catch 语句处理异常的程序文件Demo4_1.java, 源代码如下:
public class Demo3_1{
public static void main(String[] arg3) {
System.out.println(\这是一个异常处理的例子\\n\ try { inti=10; i /=0; }
catch (ArithmeticException e) {
System.out.println(\异常是:\ } finally { System.out.println(\语句被执行\ } } } ? 编译并运行程序。
注意:如果在 catch 语句中声明的异常类是 Exception,catch 语句也能正确地捕获,这是因为 Exception是ArithmeticException的父类。如果不能确定会发生哪种情况的异常,那么最好指定catch的参数为 Exception,即说明异常的类型为 Exception。 2、源文件是Demo3_2.java。要捕捉的异常是除数为零和数组下标越界。通过修改a和c[]下标值体验程序。
public class Demo3_2 { public static void main (String[] args){
- 24 -
int a, b; int c[]={1,2,3}; try{ a=10;
b=100/a;
System.out.println (\当前a值:\c[2]=200; System.out.println (\当前数组长:\ }
catch(ArithmeticException e){ System.out.println (\除零错误:\} catch(ArrayIndexOutOfBoundsException e){
System.out.println (\数组下标越界:\ finally{ System.out.println (\我是finally\ } System.out.println (\我能被正常执行到\ } }
【思考】
①先运行上面程序,观察运行结果。
②按下条件分别修改数据,编译后运行,观察输出结果,分析在try…catch块里那些语句没有被执行,为什么? 块外那些语句可被执行到,为什么? 修改a=0,保持c[2]=200; 保持a=10,修改c[3]=200, 修改a=0,修改c[3]=200;
③再添加一个catch{Exception e}{ },位置在最前面,观察编译信息,说明什么?
3、编写Demo3_3.java程序,计算两数相除并输出结果。使用两个catch子句,分别捕捉除数为0的异常和参数输入有误异常。源代码如下: import java.io.*; class Demo3_3{
publicstaticvoid main(String args[ ]) { try{
BufferedReaderstrin=newBufferedReader ( newInputStreamReader(System .in));
System .out .print(\请输入除数:\); String cl=strin .readLine(); int a=Integer .parseInt(cl); System .out .print(\请输入被除数:\); cl=strin .readLine();
int b=Integer .parseInt(cl); int c=b/a;
System .out .println(\商为:\+c); }
catch(NumberFormatException e){
System .out .println(\请输入整数!\);
- 25 -
//e .printStackTrace();
}
//捕获除数为0的异常
catch(ArithmeticException e){
System .out .println(\除数不可以0!\); //e .printstackTrace(); }
//捕获与I/O有关的异常
catch(IOException e){e.printStackTrace();}
//捕获数值转化时的异常,如不能将字符转化成数值 } }
编译并运行,当输入除数为0时,将有异常出现,当输入的不是整数时,如将30输成了3o,出现的是另一种异常。
(二)自定义异常类
4、注意throw、throws的使用。源文件是Demo3_4.java。 package sy3;
classMyException extends Exception{ public String toString(){ return \除数等于零\ } }
class DIV{ double div(double x,double y) throws MyException{ if(y==0) throw new MyException(); else return (x/y); } }
public class Demo3_4{ public static void main (String[] args) { double z; DIV d=new DIV(); try{ z=d.div(100,0); System.out.println (\当前z值:\ }catch(MyException e){ System.out.println (e.toString()); } } }
【思考】
① 修改try...catch中语句z=div.DIV(100,0)入口参数,观察运行结果。 z=d.div(100,10); z=d.div(100,0);
② 将try...catch语句注释掉,观察编译信息。
- 26 -
③ 上面条件保持,在main函数后面加上throws MyException再重新编译,能否通过?然后运行观察结果,
得到什么结论?
2.编写程序Demo3_5.java,包含自定义异常,当输入数值为13和4时抛出该异常。源代码如下: class Ex3_5 extends Exception{ Ex3_5 (String msg){ super(msg); } }
class Demo3_5{ privateintx;
voidsetX(int x) { this.x=x; }
void f1() throws Ex3_5{ if(x==13)
thrownew Ex3_5(\); elseif(x==4)
thrownew Ex3_5(\); else
System .out.println(100/x); }
publicstaticvoid main(String args[ ]) { Demo3_5 a=new Demo3_5(); try {
a.setX(5); //a.setX(13); //a.setX(4); //a.setX(0); a.f1(); }
catch(Ex3_5 e) {
System.out.println(\+e.getMessage()); } } }
【思考】编译并运行,分别取消注释上面程序中被注释的语句。当释放a.setX(13)语句后,查看运行结果,当释放a.setX(4)语句后,查看运行结果,当释放a.setX(0)语句后,查看运行结果。
四、实验练习题
1、编写Java程序,创建数组后,对数组访问时发生的数组越界.实验使用try-catch语句处理该异常。运行结果如图所示。
- 27 -
import java.io.*; publicclass Demo3_6 { }
publicstaticvoid main(String[] arg3) { }
try{
BufferedReaderstrin=newBufferedReader ( newInputStreamReader(System .in)); System.out.println(\请输入创建数组的个数:\\n\); String cl=strin.readLine();
int a=Integer .parseInt(cl);
System.out.println(\数组创建完毕!!您要查找那个元素?\\n\); String c2=strin.readLine(); }
catch(ArrayIndexOutOfBoundsException e){
System.out.println (\这里出现的错误类型是:数组下标越界!\+e); }
int b=Integer .parseInt(c2);
finally{
System.out.println (\主程序正常结束!\);
}
- 28 -
29
正在阅读:
实验五 异常处理03-17
中学语文论文12-10
财务管理期末复习题(中国人民大学版)10-10
部编教材推荐名著《飞向太空港》精导精练11-13
酒店装修施工框架协议01-24
电气工程专业实践中期检查表 - 图文04-05
simulink入门指南2012b05-18
外贸函电词汇03-11
2016年广东省初中毕业学业考试12-17
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 异常
- 实验
- 处理