java 实训指导书4

更新时间:2024-03-17 04:11:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实训项目名称:异常的使用

1、实训目的

1)了解java异常基础,学会常见病处理有异常的java程序。 2)掌握异常处理机制的应用。 2、学时 8学时 3、实训条件 硬件:计算机

软件:WIN XP操作系统,Eclipse开发平台。 地点:计算机软件实验室

4、实训内容

1)编写一个类,在main()的try语句块里抛出一个exception对象,传递一个字符串参数给exception的构造函数。在catch子句里捕获此异常对象,并打印此字符串参数。添加一个finally子句,打印一条信息以证明这里确实得到了执行。 2)使用extends关键字建立一个自定义异常类。为这个类写一个接受字符串参数的构造函数,把此参数保存在对象内部的字符串引用中,写一个方法打印此字符串。写一个try??catch子句,对这个新异常进行测试。

3)定义一个对象引用并初始化为null,尝试用此引用调用方法。把这个调用放在try?catch句子里以捕获异常。

4)编写能产生ArrayIndexOutOfBoundsException异常的代码。 5、实训步骤

任务一:先通过编写程序来处理异常,使程序更加完善。 第一题:编写一个程序,其功能是从命令行输入整数字符串,再讲该整数字符串转换为整型,输入的数据可能具有以下格式:12345,123 45,123zxc456。对这种异常进行捕获和处理。 解析:程序通过命令行输入字符串,可能会产生ioexception异常,所以程序使用catch(ioexception e)来捕获这个可能产生的异常,并且输出提示信息。而将字符串转换为整数的时候,可能会产生numberformatexception类型的异常,程序中使用catch(numberformatexception ne)来捕获这个可能出现的异常,并且提示用户输入格式有错。 操作步骤如下:

1)编写程序,参考程序如下: import java.io.*;

Public class useecxeption{

Public static void main(String args[]){

System.out.println(“请输入一个整数字符串”); Try{

Bufferedreader in =new bufferedreader(new imputstreamreader (system.in)); int a=integer.parseint(in.readline());

System.out.println(“您输入的整数是:”+a); }

Catch(ioexception e){

System.out.println(“io error”);

}

Catch (numberformatexception ne){

System.out.println(“您输入的不是一个整数字符串”):}}} 2)调试程序查看程序异常并处理异常。 第二题:设计方法Boolean prime(int n),用来判断整数n是否为素数,若为素数,返回TRUE,若不是素数,返回FALSE;若n《0,则抛出argumentoutofbound异常。

解析:程序中自定义了一个异常argumentoutofbound,用于处理输入整数小于零的情况,类中定义了一个静态方法prime(),用于判断整数n是否是素数。如果小于零,则抛出自定义的异常,argumentoutofbound。在main方法中,通过命令行读入数据m,调用方法prime判断m是否是素数,同时捕获可能出现的异常。 操作步骤如下:

1)编写程序,参考程序如下:

Pulbic class usedefineexception{

Public static Boolean prime(int n) throws argumentoutofbound{ If(n<0){

Argumentoutofbound ae=new argumentoutofbound(); Throw ae;} Else{

Boolean isprime=true; For(int i=2;i

Return isprime;}}

Public static void main(string args[]){ Int m=integer.parseint(args[0]); Try{

Boolean result=prime(m);

System.out.println(“对您输入的整数”+”是否为素数的判断结果是“+result);} Catch(argumentoutofbound e){ System.out.println(“异常名称:“+e.tostring());}}} Class argumentoutofbound extends exception{ Argumentoutofbound(){

System.out.println(“输入错误!与判断的数不能为负”);}} 2)调试程序查看程序异常并处理异常。

任务二:

第一题:编写一个类,在main()的try语句块里抛出一个exception对象,传递一个字符串参数给exception的构造函数。在catch子句里捕获此异常对象,并打印此字符串参数。添加一个finally子句,打印一条信息以证明这里确实得到了执行。 操作步骤如下:

1)编写的参考的类如下: public class Prog1{

public static void main(String[] args){ try{

throw new Exception(\ }catch(Exception e){

System.out.println(e); }finally{

System.out.println(\ } } }

2)调试程序查看程序异常并处理异常。 第二题:使用extends关键字建立一个自定义异常类。为这个类写一个接受字符串参数的构造函数,把此参数保存在对象内部的字符串引用中,写一个方法打印此字符串。写一个try??catch子句,对这个新异常进行测试。 操作步骤如下:

1)编写的参考的类如下:

class Prog2Exception extends Exception{ String str;

public Prog2Exception(String str){ super(str); this.str=str; }

public void priError(){ System.out.println(str); } }

public class Prog2{

public static void main(String[] args){ try{

throw new Prog2Exception(\ }catch(Prog2Exception e){ e.priError(); } } }

2)调试程序查看程序异常并处理异常。 任务三:

第一题:定义一个对象引用并初始化为null,尝试用此引用调用方法。把这个调用放在try?catch句子里以捕获异常。 操作步骤如下:

1)定义的参考对象如下: public class Prog3{

public static void main(String[] args){

try{

String str=null; int i=str.length(); }catch(Exception e){ System.out.println(e); }

}

}

2)调试程序查看程序异常并处理异常。

第二题:编写能产生ArrayIndexOutOfBoundsException异常的代码。 操作步骤如下:

1)编写的参考代码如下: public class Prog4{

public static void main(String[] args){

try{

int[] arrayI=new int[5];

for(int i=0;i<=arrayI.length;i++)

System.out.println(arrayI[i]); }catch(Exception e){ System.out.println(e); }

}

}

2)调试程序查看程序异常并处理异常。 6、考核方式、成绩评定标准

共10分,顺利完成Java程序的编写、编译占70%;正确执行相应的程序占30%。

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

Top