实验五 异常处理

更新时间:2023-03-13 08:45: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

本文来源:https://www.bwwdw.com/article/tifx.html

Top