当前位置:首页 > 实验指导书:实验7 异常处理与异常类
实验7异常处理与异常类
一. 实验目的及实验环境
1理解Java异常的基本概念和处理机制。
2掌握Java异常处理的方法(抛出和捕获异常;try、throw、throws、catch语句和finally的用法)
3理解异常类的作用,掌握创建异常类的方法。 二. 实验内容
1 基本内容(实验前请及时熟悉如下相关内容)
1)Java的异常处理机制 2)异常类的应用
3)常见的异常(除数为零等) 4)多异常处理 5)由方法抛出异常 6)必须要捕获的异常
2 综合实验:
2.1(Y. Daniel Liang英文版第10版P488:12.2* (InputMismatchException))(Y. Daniel Liang英文版八版P456:13.2*)(NumberFormatException) Write a program that prompts the user to read twointegers and displays their sum. Your program should prompt the user to read thenumber again if the input is incorrect.
2.2(Y. Daniel Liang英文版第10版P488:12.3* )(Y. Daniel Liang英文版八版P456:13.3*)(ArrayIndexOutBoundsException) Write a program that meets the followingrequirements:
■ Create an array with 100 randomly chosen integers.
■ Prompt the user to enter the index of the array, then display the
correspondingelement value. If the specified index is out of bounds, display the message Outof Bounds.
2.3(Y. Daniel Liang英文版第10版P488:12.4* )(Y. Daniel Liang英文版八版P456:13.4*) (IllegalArgumentException) Modify the Loan class in Listing 10.2 to
throwIllegalArgumentException if the loan amount, interest rate, or number ofyears is less than or equal to zero.
2.4 (Y. Daniel Liang英文版第10版P488:12.5* )(Y. Daniel Liang英文版八版P456:13.5*) (IllegalTriangleException) Exercise 11.1 defined the Triangle class withthree sides. In a triangle, the sum of any two sides is greater than the other side.
TheTriangle class must adhere to this rule. Create the IllegalTriangleExceptionclass, and modify the constructor of the Triangle class to throw anIllegalTriangleException object if a triangle is created with sides that violatethe rule, as follows: /** Construct a triangle with the specified sides */
public Triangle(double side1, double side2, double side3) throws IllegalTriangleException { // Implement it }
三、程序清单及结果:
1,package 异常处理; import java.util.Scanner; public class text1 {
public static void main(String[] args) { }
// TODO Auto-generated method stub int a,b;
Scanner s=new Scanner(System.in); System.out.println(\while(true) { try { }
catch(Exception e) { } }
System.out.println(\s.nextLine();
a=s.nextInt(); b=s.nextInt();
System.out.printf(\
} 2
package 异常处理; import java.util.Scanner; public class text2 { } 3
import java.lang.Math; import java.io.BufferedReader; import java.io.InputStreamReader;
//自定义异常类,继承与异常类Exception class MyException extends Exception { }
class DaiKuan
MyException(String cause,double m) { }
//若出现异常,每处理一次异常,标记flag-- shiyan3.flag--; //输出出错提示语句
System.out.println(cause+\的值为:\小于等于零\public static void main(String[] args) { }
// TODO Auto-generated method stub int[] sz=new int[100]; for(int i=0;i System.out.println(\Scanner s=new Scanner(System.in); int n=s.nextInt(); try { } catch(ArrayIndexOutOfBoundsException e){ } System.out.println(\ System.out.println(sz[n]); sz[i]=(int)Math.random()*100; { //定义变量贷款额、年利率、贷款年限、月还款额、总还款额 double DaiKuanE,NianLiLv,DaiKuanNianShu,YueKuanHuanE,ZongKuanHuanE; public void DaiKuanJiSuan() { while(shiyan3.flag==3) { try { } catch(Exception e){} try { } catch (MyException e){} try { } catch (MyException e){} try { } catch (MyException e){} finally { //若贷款额、年利率、贷款年限均大于零,则不会抛出一次异常,故flag的值不//若贷款年限不大于零,抛出异常 if(DaiKuanNianShu<=0) throw new MyException(\贷款年限\//若年利率不大于零,抛出异常 if(NianLiLv<=0) throw new MyException(\年利率\//若贷款额不大于零,抛出异常 if(DaiKuanE<=0) throw new MyException(\贷款额\//依次输入贷款额、年利率、贷款年限 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println(\请输入贷款额:\System.out.println(\请输入年利率:\System.out.println(\请输入贷款年限:\DaiKuanE=Double.parseDouble(br.readLine()); NianLiLv=Double.parseDouble(br.readLine()); DaiKuanNianShu=Double.parseDouble(br.readLine()); 变,仍然等于初值3
共分享92篇相关文档