当前位置:文档之家› 实验五---异常处理

实验五---异常处理

实验五---异常处理
实验五---异常处理

实验五异常处理

一、实验目的

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 {

int i=10;

i /=0;

}

catch (ArithmeticException e) {

System.out.println("异常是:"+e.getMessage());

}

finally {

System.out.println("finally 语句被执行");

}

}

}

编译并运行程序。

注意:如果在catch 语句中声明的异常类是Exception,catch 语句也能正确地捕获,这是因为Exception是ArithmeticException的父类。如果不能确定会发生哪种情况的异常,那么最好指定catch 的参数为Exception,即说明异常的类型为Exception。

2、源文件是Demo3_2.java。要捕捉的异常是除数为零和数组下标越界。通过修改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);

c[2]=200;

System.out.println ("当前数组长:"+c.length);

}

catch(ArithmeticException e){

System.out.println ("除零错误:"+e); }

catch(ArrayIndexOutOfBoundsException e){

System.out.println ("数组下标越界:"+e); }

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{

public static void main(String args[ ]) {

try{

BufferedReader strin=new BufferedReader ( new InputStreamReader(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("请输入整数!"); //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;

class MyException 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值:"+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语句注释掉,观察编译信息。

③上面条件保持,在main函数后面加上throws MyException再重新编译,能否通过?然后运行观察

结果,得到什么结论?

2.编写程序Demo3_5.java,包含自定义异常,当输入数值为13和4时抛出该异常。源代码如下:class Ex3_5 extends Exception{

Ex3_5 (String msg){

super(msg);

}

}

class Demo3_5{

private int x;

void setX(int x) {

this.x=x;

}

void f1() throws Ex3_5{

if(x==13)

throw new Ex3_5("I don’t like 13!");

else if(x==4)

throw new Ex3_5("I don’t 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.setX(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 ("主程序正常结束!");

}

}

}

相关主题
文本预览
相关文档 最新文档