当前位置:文档之家› 第6章 异常实验一

第6章 异常实验一

编写代码,代码清单如下:
public class CatchDemo{
private int denominator, numerator, quotient;
public CatchDemo () {
denominator = 0;
numerator = 12;
try {
quotient = quotient(numerator,denominator);
try{
System.out.println("mathodB 正常返回");
return;
}finally {
System.out.println("执行 mathodB 的 finally");
}
}
public static void main(String args[]) {
try {
mathodA();
}
public static void main(String args[]) {
try {
mathod();
}catch (NullPointerException ex) {
System.out.println("在 main 中捕获空指针异常:"+ex);
}
}
}
运行程序,查看运行结果。
finally子句
第6章Java异常处理
实验一Java异常处理机制
实验目的
掌握基本异常的处理机制。
熟悉try语句与catch语句的搭配使用。
了解有异常处理与没有异常处理的差别。
多重catch语句的使用。
使用throws声明异常和throw抛出异常。
实验指导
除0异常
编写程序,实验除0异常。程序运行结果如下图所示。
图6.1 DivideByZero类运行结果
System.out.print("Quotient is " + quotient);
}catch(IndexOutOfBoundsException ex){
System.out.println("I found exception " + ex.toString());
}
}
public int quotient ( int numerator,int denominator ){
} catch (Exception e) {
mathodB();
}
}
}
运行程序,查看运行结果。
实验习题
下面程序实现系统运行时异常的捕捉,补充代码并给出程序运行结果
public class TestFinally{
static double divide(double r1,double r2) 【此处补充代码】{ // 抛出运行时异常
throw ex;
}
}
public static void main(String args[]) {
try {
throwMethod();
}catch (NullPointerException ex) {
System.out.println("再次捕获:"+ex);
}
}
}
运行程序,查看运行结果。
if (r2 == 0){
throw new RuntimeException("denominator cannot be zero");
}
System.out.println("divide() continues");
return r1/r2;
}
public static void main(String[] args){
}catch(ArrayIndexOutOfBoundsException e){
System.out.println ("数组下标越界:"+e);
}catch(NullPointerException e){
System.out.println ("空指针:"+e);
}catch(NumberFormatException e){
throws子句声明异常
throws子句一般用来表明在使用该方法时可能会抛出异常,但该方法并不捕获和处理异常,而由系统或调用该方法的方法中处理异常。编写throws子句程序,程序运行结果如下图所示。
图6.3 ThrowsDemo类运行结果
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,建立对应的类(类名ThrowsDemo)。
编写程序,实验finally子句的使用。程序实现效果如下图所示。
图6.4DemoFinally类运行结果
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的类(类名DemoFinally)。
编写代码,代码清单如下:
classDemoFinally{
static void mathodA() {
try {
System.out.println("\nmathodA 抛出一个异常");
throw new RuntimeException();
}finally {
System.out.println("执行 mathodA 的 finally");}
}
static void mathodB() {
String s="sss";
System.out.println("字符串s长度:"+s.length());
c=Integer.parseInt("123");
System.out.println("转换后c的值:"+c);
}catch(ArithmeticException e){
System.out.println ("除零错误:"+e);
public class ThrowDemo {
static void throwMethod() {
try {
throw new NullPointerException("空指针异常");
}catch (NullPointerException ex) {
System.out.println("\n 在 throwMethod 方法中捕获一个"+ex.getMessage());
double d1=3.0,d2=0;
try{
double result =ቤተ መጻሕፍቲ ባይዱdivide(d1,d2);
System.out.println("After divide() calling,result is:"+result);
}
【此处补充代码】// 捕捉运行时异常
System.out.println("catch:"+ex);
return numerator / denominator;
}
public static void main( String args[] ){
DivideByZero application = new DivideByZero();
}
}
运行程序,查看运行结果。
try…catch语句处理异常
在上例中加入try/catch语句块,以处理除数为0时异常。程序运行结果显示“I found exception ng.ArithmeticException: / by zero”
denominator = 0;
numerator = 12;
quotient = quotient(numerator,denominator);
System.out.print("Quotient is " + quotient);
}
public int quotient ( int numerator,int denominator ){
}
【此处补充代码】// 始终执行的语句
System.out.println("finally");
}
System.out.println("program continues");
}
}
参考下面的程序,修改程序,捕获相关异常,使得程序能正常运行。[提示:用错误数据测试,即可得到异常类名,如运行时主方法参数输入abc测试]
多catch块异常处理
通过修改a、数值m的下标值、字符串s和参数c的值,捕捉除数为零、数组下标越界、空指针、类型转换异常。
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,并建立对应类(类名SeveralCatchDemo)。
编写代码,代码清单如下:
public class SeveralCatchDemo{
System.out.println ("类型转换错误:"+e);
}
System.out.println ("可以正常执行到语句");
}
}
运行程序,查看运行结果。
思考
分别修改不同参数,编译后运行,观察输出结果,分析在try…catch块里那些语句没有被执行,为什么?块外那些语句可被执行到,为什么?
如果再在最前面位置添加一个catch{Exception e}{ },会出现什么情况?
}catch(ArithmeticException ex){
相关主题