第6章 异常实验一

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
编写代码,代码清单如下:
public class CatchDemo{
private int denominator, numerator, quotient;
public CatchDemo () {
denominator = 0;
numerator = 12;
try {
quotient = quotient(numerator,denominator);
public DivideByZeroCatch() {
denominator = 0;
numerator = 12;
try {
quotient = quotient(numerator,denominator);
System.out.print("Quotient is " + quotient);
}catch(ArithmeticException ex){
System.out.println("I found exception " + ex.toString());
}
}
public int quotient ( int numerator,int denominator ){
return numerator / denominator;
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,并建立对应类(类名DivideByZero)。
编写代码,代码清单如下:
public class DivideByZero{
private int denominator, numerator, quotient;
public DivideByZero() {
第6章Java异常处理
实验一Java异常处理机制
实验目的
掌握基本异常的处理机制。
熟悉try语句与catch语句的搭配使用。
了解有异常处理与没有异常处理的差别。
多重catch语句的使用。
使用throws声明异常和throw抛出异常。
实验指导
除0异常
编写程序,实验除0异常。程序运行结果如下图所示。
图6.1 DivideByZero类运行结果
public static void main (String[] args){
int a,b,c;
int m[]={1,2,3};
try{
a=10;
b=100/a;
System.out.println("当前a值:"+a);
m[2]=100;
System.out.println("当前数组长:"+m.length);
多catch块异常处理
通过修改a、数值m的下标值、字符串s和参数c的值,捕捉除数为零、数组下标越界、空指针、类型转换异常。
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,并建立对应类(类名SeveralCatchDemo)。
编写代码,代码清单如下:
public class SeveralCatchDemo{
编写程序,实验finally子句的使用。程序实现效果如下图所示。
图6.4DemoFinally类运行结果
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的类(类名DemoFinally)。
编写代码,代码清单如下:
classDemoFinally{
static void mathodA() {
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);
throw ex;
}
}
public static void main(String args[]) {
try {
throwMethod();
}catch (NullPointerException ex) {
System.out.println("再次捕获:"+ex);
}
}
}
运行程序,查看运行结果。
return numerator / denominator;
}
public static void main( String args[] ){
CatchDemo application = new CatchDemo();
}
}
运行程序,查看运行结果。
说明
程序试图捕获一个异常类为IndexOutOfBoundsException 的异常,但发生的异常却是ArithmeticException 类,所以,程序可以通过编译但在运行时,系统会给出异常报告:报告所发生的但没有被捕获的异常。
System.out.print("Quotient is " + quotient);
}catch(IndexOutOfBoundsException ex){
System.out.println("I found exception " + ex.toString());
}
}
public int quotient ( int numerator,int denominator ){
System.out.println ("类型转换错误:"+e);
}
System.out.println ("可以正常执行到语句");
}
}
运行程序,查看运行结果。
思考
分别修改不同参数,编译后运行,观察输出结果,分析在try…catch块里那些语句没有被执行,为什么?块外那些语句可被执行到,为什么?
如果再在最前面位置添加一个catch{Exception e}{ },会出现什么情况?
denominator = 0;
numerator = 12;
quotient = quotient(numerator,denominator);
System.out.print("Quotient is " + quotient);
}
public int quotient ( int numerator,int denominator ){
throws子句声明异常
throws子句一般用来表明在使用该方法时可能会抛出异常,但该方法并不捕获和处理异常,而由系统或调用该方法的方法中处理异常。编写throws子句程序,程序运行结果如下图所示。
图6.3 ThrowsDemo类运行结果
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,建立对应的类(类名ThrowsDemo)。
public class ThrowDemo {
static void throwMethod() {
try {
throw new NullPointerException("空指针异常");
}catch (NullPointerException ex) {
System.out.println("\n 在 throwMethod 方法中捕获一个"+ex.getMessage());
} catch (Exception e) {
mathodB();
}
}
}
运行程序,查看运行结果。
实验习题
下面程序实现系统运行时异常的捕捉,补充代码并给出程序运行结果
public class TestFinally{
static double divide(double r1,double r2) 【此处补充代码】{ // 抛出运行时异常
}catch(ArrayIndexOutOfBoundsException e){
System.out.println ("数组下标越界:"+e);
}catch(NullPointerException e){
System.out.println ("空指针:"+e);
}catch(NumberFormatException e){
try{
System.out.println("mathodB 正常返回");
return;
}finally {
System.out.println("执行 mathodB 的 finally");
}
}
public static void main(String args[]) {
try {
mathodA();
throw语句抛出异常
Java抛出异常也可以通过使用throw语句实现。编写程序,实验throw抛出异常。程序运行结果如下图所示。
图6.2 ThrowDemo类运行结果
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,建立对应的类(类名ThrowDemo)。
编写代码,代码清单如下:
}
public static void main(String args[]) {
try {
mathod();
}catch (NullPointerException ex) {
System.out.println("在 main 中捕获空指针异常:"+ex);
}
}
}
运行程序,查看运行结果。
finally子句
}
【此处补充代码】// 始终执行的语句
System.out.println("finally");
}
System.out.println("program continues");
}
}
参考下面的程序,修改程序,捕获相关异常,使得程序能正常运行。[提示:用错误数据测试,即可得到异常类名,如运行时主方法参数输入abc测试]
return numerator / denominator;
}
public static void main( String args[] ){
DivideByZero application = new DivideByZero();
}
}
运行程序,查看运行结果。
try…catch语句处理异常
在上例中加入try/catch语句块,以处理除数为0时异常。程序运行结果显示“I found exception java.lang.ArithmeticException: / by zero”
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,并建立对应类(类名DivideByZeroCatch)。
编写代码,代码清单如下:
public class DivideByZeroCatch{
private int denominator, numerator, quotient;
if (r2 == 0){
throw new RuntimeException("denominator cannot be zero");
}
System.out.println("divide() continues");
return r1/r2;
}
public static void main(String[] args){
try {
System.out.println("\nmathodA 抛出一个异常");
throw new RuntimeException();
}finally {
System.out.println("执行 mathodA 的 finally");}
}
static void mathodB() {
}
public static void main( String args[] ){
DivideByZeroCatch application = new DivideByZeroCatch();
}
}
运行程序,查看运行结果。
异常类型不匹配
修改实验(二)捕捉异常的类型,查看程序运行结果。
实验步骤
在UltraEdit“项目文件夹ch06”中创建一个新的java文件,并建立对应类(类名CatchDemo)。
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 ThrowsDemo{
static void mathod() throws NullPointerException {
System.out.println("在 mathod Βιβλιοθήκη Baidu抛出一个空指针异常");
throw new NullPointerException();
相关文档
最新文档