java 实训指导书4
更新时间:2024-01-01 21:56:01 阅读量: 教育文库 文档下载
- java推荐度:
- 相关推荐
实训项目名称:异常的使用
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%。
正在阅读:
java 实训指导书401-01
连云港专业技术人员继续教育 - 公共危机管理试卷答案10-24
无线Mesh网络的跨层设计理论与关键技术04-17
如何规避工程承包项目中的风险08-19
电子电工第一章练习题12-01
姚期智:从普林斯顿到水木清华 - 图文11-23
2018年中国硫酸钙行业方向研究报告目录03-15
多组份工业废渣配料煅烧优质水泥熟料06-09
电信机务员数据通信设备及维护篇08-19
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 指导书
- java
- 氩电联焊工艺-低压甲醇
- 中国科学技术大学(1) - 图文
- 加强和改进财政审计工作的建议与思考
- 新版pep四年级上unit5单元测试
- 2014高考语文全国卷2(含答案)
- 2019年八年级语文上册 第一单元 第1课 消息二则备课资料 新人教版 - 图文
- Photoshop试卷B卷,多3,30
- 某工程招标文件修改 - 图文
- 小学2012-2013学年教研工作计划1
- 小学六年级语文教师工作总结
- 中国未来20年技术预见 目录
- 投款流程管理制度
- 2013高考山东文综试题及答案
- 公路工程质量鉴定办法
- 北斗导航定位系统应用领域分析
- 2020年上海国家电网社会招聘考试内容(1840)
- 2017届高三文科数学模拟试卷精彩试题汇编17 含解析
- 浅论影响英语谚语翻译的因素
- 七年级数学不等式解应用题练习
- 2014年国家公务员面试每日一练(48)