实验五---异常处理

上传人:我*** 文档编号:132127444 上传时间:2020-05-12 格式:DOCX 页数:6 大小:72.95KB
返回 下载 相关 举报
实验五---异常处理_第1页
第1页 / 共6页
实验五---异常处理_第2页
第2页 / 共6页
实验五---异常处理_第3页
第3页 / 共6页
实验五---异常处理_第4页
第4页 / 共6页
实验五---异常处理_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《实验五---异常处理》由会员分享,可在线阅读,更多相关《实验五---异常处理(6页珍藏版)》请在金锄头文库上搜索。

1、实验五 异常处理一、实验目的1. 学会利用Try-catch-finally语句来捕获和处理异常;2. 掌握自定义异常类的方法。二、实验要求1. 通过编程理解系统异常处理的机制和创建自定义异常的方法。2. 掌握基础类。三、实验内容(一)异常处理机制1. 编写使用 trycatch 语句处理异常的程序文件Demo4_1.java,源代码如下: public class Demo3_1 public static void main(String arg3) System.out.println(这是一个异常处理的例子n); try int i=10; i /=0; catch (Arithmet

2、icException e) System.out.println(异常是:+e.getMessage(); finally System.out.println(finally 语句被执行); l 编译并运行程序。 注意:如果在 catch 语句中声明的异常类是 Exception,catch 语句也能正确地捕获,这是因为 Exception是ArithmeticException的父类。如果不能确定会发生哪种情况的异常,那么最好指定catch的参数为 Exception,即说明异常的类型为 Exception。2、源文件是Demo3_2.java。要捕捉的异常是除数为零和数组下标越界。通过

3、修改a和c下标值体验程序。public class Demo3_2 public static void main (String args)int a, b;int c=1,2,3;try a=10; b=100/a; System.out.println (当前a值:+a); c2=200; System.out.println (当前数组长:+c.length); catch(ArithmeticException e) System.out.println (除零错误:+e); catch(ArrayIndexOutOfBoundsException e) System.out.pri

4、ntln (数组下标越界:+e); finally System.out.println (我是finally); System.out.println (我能被正常执行到);【思考】 先运行上面程序,观察运行结果。 按下条件分别修改数据,编译后运行,观察输出结果,分析在trycatch块里那些语句没有被执行,为什么? 块外那些语句可被执行到,为什么?修改a=0, 保持c2=200;保持a=10,修改c3=200,修改a=0, 修改c3=200; 再添加一个catchException e ,位置在最前面,观察编译信息,说明什么?3、编写Demo3_3.java 程序,计算两数相除并输出结果。

5、使用两个catch子句,分别捕捉除数为0的异常和参数输入有误异常。源代码如下:import java.io.*;class Demo3_3public static void main(String args ) tryBufferedReader strin=new BufferedReader ( new InputStreamReader(System .in);System .out .print(请输入除数:);String cl=strin .readLine();int a=Integer .parseInt(cl);System .out .print(请输入被除数:);cl=

6、strin .readLine();int b=Integer .parseInt(cl);int c=b/a;System .out .println(商为:+c);catch(NumberFormatException e)System .out .println(请输入整数!);/e .printStackTrace();/捕获除数为0的异常catch(ArithmeticException e)System .out .println(除数不可以0!); /e .printstackTrace(); /捕获与I/O有关的异常catch(IOException e)e.printStac

7、kTrace(); /捕获数值转化时的异常,如不能将字符转化成数值 编译并运行,当输入除数为0时,将有异常出现,当输入的不是整数时,如将30输成了3o,出现的是另一种异常。(二)自定义异常类4、注意throw、throws的使用。源文件是Demo3_4.java。package sy3;class MyException extends Exceptionpublic String toString()return 除数等于零; class DIVdouble div(double x,double y) throws MyExceptionif(y=0) throw new MyExcept

8、ion();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值:+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.ca

9、tch语句注释掉,观察编译信息。 上面条件保持,在main函数后面加上throws MyException再重新编译,能否通过?然后运行观察结果,得到什么结论?2编写程序Demo3_5.java,包含自定义异常,当输入数值为13和4时抛出该异常。源代码如下:class Ex3_5 extends Exception Ex3_5 (String msg) super(msg); class Demo3_5private int x;void setX(int x) this.x=x; void f1() throws Ex3_5 if(x=13) throw new Ex3_5(I dont l

10、ike 13!);else if(x=4)throw new Ex3_5(I dont like 4!);else System .out.println(100/x);public static void 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(get message:+e.getMessage();【思考】编译并运行,分别取消注释上面程序中被注释的语句。当释放a.s

11、etX(13)语句后,查看运行结果,当释放a.setX(4)语句后,查看运行结果,当释放a.setX(0)语句后,查看运行结果。四、实验练习题1、编写Java程序,创建数组后,对数组访问时发生的数组越界.实验使用try-catch语句处理该异常。运行结果如图所示。import java.io.*;public class Demo3_6 public static void main(String arg3) try BufferedReader strin=new BufferedReader ( new InputStreamReader(System .in); System.out.println(请输入创建数组的个数:n); String cl=strin.readLine(); int a=Integer .parseInt(cl); System.out.println(数组创建完毕!您要查找那个元素?n); String c2=strin.readLine(); int b=Integer .parseInt(c2); catch(ArrayIndexOutOfBoundsException e) System.out.println (这里出现的错误类型是:数组下标越界!+e); finally System.out.println (主程序正常结束!); 29

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号