




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
信息科学与工程学院 Java程序设计 上机实验报告专业班级 姓 名 学 号 实验时间 指导教师 成 绩 实验名称实验四 异常类的定义及处理实验目的1) 了解异常处理方法。2) 熟悉并掌握常见异常的捕获方法。3) 熟悉JDK中已经定义的若干异常类的层次结构。4) 掌握自定义异常类的创建方法。主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)实验内容:1、编写程序实现如下功能:生成并捕获到NegativeArraySizeException和IndexOutOfBoundsException类型的异常,并显示捕获到的异常信息。然后在此基础上生成并捕获到NullPointerException类型的异常,并显示捕获到的异常信息。实验步骤:步骤(1):编写一个包含main方法的Application类TestException,然后定义一个方法void arraySize()生成并捕获NegativeArraySizeException异常。步骤(2):添加一个方法void outofBound()生成并捕获IndexOutOfBoundsException异常。步骤(3):添加一个方法void nullPointer()生成并捕获IndexOutOfBoundsException异常。步骤(4):在main方法中分别调用以上三个方法。步骤(5):将文件保存为TestException.java,然后编译、调试应用程序。步骤(6):将outofBound()方法中捕获异常的语句注释掉,重新编译程序,看看会不会有什么语法错误?如果没错误,执行程序看结果有什么不同?步骤(7):将array方法重新定义为如下形式:void arraySize() throws NegativeArraySizeException然后修改arraySize方法中捕获NegativeArraySizeException异常的语句执行部分。主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)程序代码:public class TestException public static void main(String args) arraySize(); outofBound(); nullPointer(); static void arraySize() throws NegativeArraySizeException try int Array = new int-1; catch(NegativeArraySizeException e) System.out.println (数组大小异常:+e); static void outofBound() try int i; int Array=new int3; for(i=0;i=3;i+) Arrayi=i; catch(IndexOutOfBoundsException e) System.out.println (数据溢出:+e); static void nullPointer() try String s; int a; s=null; a=s.length(); catch(NullPointerException e) System.out.println (调用对象为空:+e); 主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)程序截图:1、 编写程序实现如下功能:计算两个数之和,参与求和运算的每个数的值都必须在10-20之间,当任意一个数超出范围时,抛出自己的异常。实验步骤:步骤(1):基于系统异常类Exception,定义自己的异常类NumberRangeException。步骤(2):定义包含main方法的Application类SelfException。步骤(3):在SelfException类中添加公共方法:Public static int selfExceptionTest(int int1,int int2) throws NumberRangeException使之能在求int1,int2两个数和之前检查两个数的数值范围,若符合条件则求和,否则抛出异常NumberRangeException。步骤(4):在main方法中调用selfExceptionTest方法。步骤(5):保存文件为SelfException.java,然后编译并调试程序。步骤(6):修改main方法中调用selfExceptionTest方法的实参,看看程序的运行结果有什么不同。程序代码:class NumberRangeException extends Exceptionstatic int a,b;public NumberRangeException(String message,int a,int b)super(message);this.a=a;this.b=b;主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等) public class SelfException public static void selfExceptionTest(int a,int b) throws NumberRangeExceptionif(a20)|(b20)throw new NumberRangeException(数值超出指定范围, a, b);System.out.println(a=+a+ +b=+b+n+sum=+(a+b); public void GetSum()tryselfExceptionTest(1,100);catch(NumberRangeException e)System.out.println(a=+NumberRangeException.a+ b=+NumberRangeException.b+n+e);public static void main(String args)SelfException num=new SelfException();num.GetSum();程序截图:(第一次设a=1,b=100;第二次设a=15,b=15。)主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)思考题:1、翻译下列常用异常类的描述信息OutOfMemoryError A class instance creation expression, array creation expression , or string concatenation operatior expression throws an OutOfMemoryError if there is insufficient memory available.如果没有足够的可用内存,一个类的实例创建表达式,数组创建表达式,或字符串串联操作表达会抛出一个OutOfMemoryError。 NegativeArraySizeException An array creation expression throws a NegativeArraySizeException if the value of any dimension expression is less than zero.一个数组创建表达式会抛出一个NegativeArraySizeException如果任何维度表达的数值小于零。NullPointerException A field access throws a NullPointerException if the value of the object referenceexpression is null. A method invocation expression that invokes an instance method throws a NullPointerException if the target reference is null. An array access throws a NullPointerException if the value of the array referenceexpression is null. 一个字段访问抛出NullPointerException如果对象的值引用表达式是null。一个方法调用,调用一个实例方法表达抛出NullPointerException如果目标参考是null。一个数组访问抛出NullPointerException如果值的数组引用表达式是null。ArrayIndexOutOfBoundsException An array access throws an ArrayIndexOutOfBoundsException if the value of the array index expression is negative or greater than or equal to the length of the array. 一个数组访问抛出一个ArrayIndexOutOfBoundsException如果值的数组索引表达式是负面的或大于或等于数组的长度。主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)ClassCastException Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException: Object x = new Integer(0); System.out.println(String)x);抛出指示代码试图创建一个对象的一个子类,它不是一个实例。例如,下面的代码生成一个ClassCastException异常: Object x = new Integer(0); System.out.println(String)x)ArithmeticException Thrown when an exceptional arithmetic condition has occurred. For example, an integer divide by zero throws an instance of this class. 一个非凡的算术条件时抛出发生。例如,一个整数”除以零”抛出一个这个类的实例。ArrayStoreExceptionAn assignment to an array component of reference type throws an ArrayStoreException when the value to be assigned is not compatible with the component type of the array.一个赋值数组组件的引用类型抛出一个ArrayStoreException当价值分配是不兼容的组件类型数组。NoSuchFieldExceptionSignals that the class doesnt have a field of a specified name.提示:在类中没有一个方法指定为该名称。NoSuchMethodExceptionThrown when a particular method cannot be found.抛出它,当该方法不能被找到的时候。NumberFormatExceptionThrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format. 抛出它,当确定应用程序试图将字符串转换成数值类型之一,但是,字符串没有适当主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)的格式。FileNotFoundExceptionSignals that an attempt to open the file denoted by a specified pathname has failed.提示:试图打开文件,表明一个指定的路径名已经失败了。IOExceptionSignals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. 提示:一个I / O信号发生某种异常。这个类是一般类的异常产生的失败或中断I / O操作。2、编写一个程序,用于根据用户输入的命令行参数数量来计算长方形、正方形、三角形的面积。如果输入的参数个数为1、2、3则它们应分别对应正方形、长方形、三角形,如果参数值为0,则异常处理方法显示错误消息。提示:自己定义一个异常类,表示参数个数0这一异常。然后定义一个抽象的父类,并提供一个抽象的方法area(),再派生出三个子类,重写area方法,最后在main方法中编写测试逻辑。import java.util.Scanner;class MyException extends Exception public void MyException(int i) if(i=0) System.out.println(输入出错!请重新输入:); abstract class area abstract void area();class RectangleArea extends area int area(int a,int b) int area=a*b;return area;主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)class SquareArea extends area int area(int a) int area=a*a;return area; void area() class TriangleArea extends area double area(int a,int b,int c) double p=0.5*(a+b+c);double area=Math.sqrt( p*(p-a)*(p-b)*(p-c);return area; void area()public class CountArea extends MyException static int a,b,c,i; static int s=a,b,c; static MyException me=new MyException(); public static void main(String args) throws MyException Scanner sca=new Scanner(System.in); if(sca.equals(null) me.MyException(0); elsewhile(true) System.out.println(输入若干整数,用空格隔开,以Enter结束); String num=sca.nextLine(); num=num.toLowerCase(); if(num.equals(Enter) break; String strs=num.split( ); i=strs.length; if(i=1) SquareArea sa=new SquareArea();主 要 实 验 记 录 及 个 人 小 结 (包括部分实验源程序、调试结果及实验结果分析等)int area=sa.area(Integer.parseInt(strs0);System.out.println(正方形面积为+a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湖南电气职业技术学院《景区运营管理》2024-2025学年第一学期期末试卷
- 江西艺术职业学院《资源循环工程基础实验》2024-2025学年第一学期期末试卷
- 二年级数学计算题专项练习集锦
- 广东省六校2025届高三5月联考生物试题(解析版)
- 六年级数学比例复习单元试卷及答案
- 2025年行业工程师招聘面试题集
- 2025年行业招聘面试题专家出题趋势分析与预测题解答
- 2025年经济师中级职称考试模拟题及答题技巧
- 2025年烈士纪念设施保护工作实践案例分析模拟题集及答案
- 珠宝销售基础知识培训课件
- 运输咨询服务合同协议
- 钱大妈合同协议书
- 育苗基地转让合同协议
- 静脉治疗的质量管理
- 脑-耳交互神经调控-全面剖析
- 2024版原醛症诊断治疗的专家共识解读
- 教师名师笔试题库及答案
- 矿用圆环链简介
- 连锁公司发票管理制度
- 中级四级计算机程序员技能鉴定理论考试题(附答案)
- 学校食堂员工薪资方案
评论
0/150
提交评论