当前位置:
文档之家› 武汉科技大学 JAVA实验报告 实验四
武汉科技大学 JAVA实验报告 实验四
实 OutOfMemoryError
验
记
A class instance creation expression, array creation expression , or
录 string concatenation operatior expression throws an OutOfMemoryError if there
包
括
}catch(NumberRangeException e){
部 分
System.out.println(""a="+NumberRangeException.a+"
实 b="+NumberRangeException.b+"\n"+e");
验
源
}
程 序
}
、
public static void main(String[] args){
调
试 步骤(6):将 outofBound()方法中捕获异常的语句注释掉,重新编译程序,看看会
结
果
不会有什么语法错误?如果没错误,执行程序看结果有什么不同?
及
实 步骤(7):将 array 方法重新定义为如下形式:
验
结
void arraySize() throws NegativeArraySizeException{……}
实 验 步骤(2):定义包含 main 方法的 Application 类 SelfException。
结 果
步骤(3):在 SelfException 类中添加公共方法:Public static int selfExceptionTest
分 析
(int int1,int int2) throws NumberRangeException 使之能在求 int1,int2 两个
调
试
SelfException num=new SelfException();
结
果
num.GetSum();
及 实
}
验}
结
果
分 析
程序截图:(第一次设 a=1,b=100;第二次设 a=15,b=15。)
等
)
注:“实验记录及个人小结”部分不够可另附页或在背面续写
第
页
主 思考题:
要 1、翻译下列常用异常类的描述信息
信息科学与工程学院 《Java 程序设计》 上机实验报告
专业班级 实验时间
姓名 指导教师
学号 成绩
实验
实验四 异常类的定义及处理
名称
1) 了解异常处理方法。
2) 熟悉并掌握常见异常的捕获方法。
实验
目的 3) 熟悉 JDK 中已经定义的若干异常类的层次结构。
4) 掌握自定义异常类的创建方法。
主 实验内容:
要 1 、 编 写 程 序 实 现 如 下 功 能 : 生 成 并 捕 获 到 NegativeArraySizeException 和 实
IndexOutOfBoundsException 类型的异常,并显示捕获到的异常信息。然后在此基础 验 记 上生成并捕获到 NullPointerException 类型的异常,并显示捕获到的异常信息。
录 {throw new NumberRangeException("数值超出指定范围", a, b);
及
个
}
人
System.out.println("a="+a+" "+"b="+b+"\n"+"sum="+(a+b));
小
}
结
public void GetSum(){
(
try{selfExceptionTest(1,100);
结果有什么不同。
程序代码:
class NumberRangeException extends Exception{ static int a,b; public NumberRangeException(String message,int a,int b){ super(message); this.a=a; this.b=b;
源
程
一个非凡的算术条件时抛出发生。例如,一个整数”除以零”抛出一个这个类的
序 、 实例。
调 试 ArrayStoreException
结
果
An assignment to an array component of reference type throws an
及 实
ArrayStoreException when the value to be assigned is not compatible with the
调
试 reference expression is null.
结
果
A method invocation expression that invokes an instance method throws a
及
实 NullPointerException if the target reference is null.
及 is insufficient memory available.
个
人
如果没有足够的可用内存,一个类的实例创建表达式,数组创建表达式,或字符
小 串串联操作表达会抛出一个 OutOfMemoryError。
结 NegativeArraySizeException
(
An array creation expression throws a NegativeArraySizeException if
) 一个方法调用,调用一个实例方法表达抛出 NullPointerException 如果目标参
考是 null。
一个数组访问抛出 NullPointerException 如果值的数组引用表达式是 null。
ArrayIndexOutOfBoundsException
An array access throws an ArrayIndexOutOfBoundsException if the value of
录 实验步骤:
及 个 步骤(1):编写一个包含 main 方法的 Application 类 TestException,然后定义一
人
个方法
小
void arraySize(){……}生成并捕获 NegativeArraySizeException 异常。
结 步骤(2):添加一个方法
(
包
void outofBound(){……}生成并捕获 IndexOutOfBoundsException 异常。
等 )
数和之前检查两个数的数值范围,若符合条件则求和,否则抛出异常
NumberRangeException。
步骤(4):在 main 方法中调用 selfExceptionTest 方法。
步骤(5):保存文件为 SelfException.java,然后编译并调试程序。
步骤(6):修改 main 方法中调用 selfExceptionTest 方法的实参,看看程序的运行
注:“实验记录及个人小结”部分不够可另附页或在背面续写
第
页
主
}
要}
实 public class SelfException {
验
public static void selfExceptionTest(int a,int b) throws NumberRangeException{
记
if((a<10||a>20)||(b<10||b>20))
验
结
An array access throws a NullPointerException if the value of the array
果 分 reference expression is null.
析
等
一个字段访问抛出 NullPointerException 如果对象的值引用表达式是 null。
结
果
System.out.println ("数据溢出:"+e);
分
析
}
等
)
}
static void nullPointer() {
try{
String s;
int a;
s=null;
a=s.length();
}catch(NullPointerException e){
System.out.println ("调用对象为空:"+e);
the array index expression is negative or greater than or equal to the length
of the array.
一个数组访问抛出一个 ArrayIndexOutOfBoundsException 如果值的数组索引表
达式是负面的或大于或等于数组的长度。
验
arraySize();
记
outofBound();
录
nullPointer(); 及
个
}
人
static void arraySize() throws NegativeArraySizeException {
小
try{
结
int Array[] = new int[-1];
(
}catch(NegativeArraySizeException e){