资源描述
实验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 two integers and displays their sum. Your program should prompt the user to read the number 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 following requirements:
■ Create an array with 100 randomly chosen integers.
■ Prompt the user to enter the index of the array,then display the corresponding element value. If the specified index is out of bounds,display the message Out of 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 throw IllegalArgumentException if the loan amount,interest rate,or number of years 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 with three sides. In a triangle,the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class,and modify the constructor of the Triangle class to throw an IllegalTriangleException object if a triangle is created with sides that violate the 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("read two integers:");
while(true)
{
try {
a=s.nextInt();
b=s.nextInt();
System.out.printf("%d",a+b);
}
catch(Exception e)
{
System.out.println("read the number again");
s.nextLine();
}
}
}
}
2
package 异常解决;
import java.util.Scanner;
public class text2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] sz=new int[100];
for(int i=0;i<sz.length;i++)
{
sz[i]=(int)Math.random()*100;
}
System.out.println("enter the index of the array:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
try {
System.out.println(sz[n]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("System.out.println");
}
}
}
3
import java.lang.Math;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//自定义异常类,继承与异常类Exception
class MyException extends Exception
{
MyException(String cause,double m)
{
//若浮现异常,每解决一次异常,标记flag--
shiyan3.flag--;
//输出出错提示语句
System.out.println(cause+"值为:"+m+"不大于等于零");
}
}
class DaiKuan
{
//定义变量贷款额、年利率、贷款年限、月还款额、总还款额
double DaiKuanE,NianLiLv,DaiKuanNianShu,YueKuanHuanE,ZongKuanHuanE;
public void DaiKuanJiSuan()
{
while(shiyan3.flag==3)
{
try
{
//依次输入贷款额、年利率、贷款年限
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());
}
catch(Exception e){}
try
{
//若贷款额不不不大于零,抛出异常
if(DaiKuanE<=0)
throw new MyException("贷款额",DaiKuanE);
}
catch (MyException e){}
try
{
//若年利率不不不大于零,抛出异常
if(NianLiLv<=0)
throw new MyException("年利率",NianLiLv);
}
catch (MyException e){}
try
{
//若贷款年限不不不大于零,抛出异常
if(DaiKuanNianShu<=0)
throw new MyException("贷款年限",DaiKuanNianShu);
}
catch (MyException e){}
finally
{
//若贷款额、年利率、贷款年限均不不大于零,则不会抛出一次异常,故flag值不变,依然等于初值3
if(shiyan3.flag==3)
{
YueKuanHuanE=DaiKuanE*(NianLiLv/12.0)/(1.0-Math.pow((1.0/(1.0+NianLiLv/12.0)),(DaiKuanNianShu*12)));//计算月还款额
ZongKuanHuanE=YueKuanHuanE*DaiKuanNianShu*12;//计算总还款额
System.out.println("月还款额为:"+(float)YueKuanHuanE);//输出:月还款额以及总还款额
System.out.println("总还款额为:"+(float)ZongKuanHuanE);
shiyan3.flag=0;
}
else
{
//若浮现异常,则至少进行一次异常解决,flag值变化
System.out.println("\n您输入有误.请重新输入您贷款数额贷款年利率和贷款年限:\n");
//若浮现异常,重置flag=3;再次循环
shiyan3.flag=3;
}
}
}
}
}
public class shiyan3
{
//定义静态全局变量,作为与否浮现异常标记
static int flag=3;
public static void main(String args[])
{
DaiKuan dk=new DaiKuan();
dk.DaiKuanJiSuan();
}
}
4
package 异常解决;
import java.util.Scanner;
public class text4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
double ss1 = 1.5;double ss2 = 0.1;double ss3 = 1.2;
ss1=s.nextDouble();
ss2=s.nextDouble();
ss3=s.nextDouble();
try
{
TriangleSideException tri = new TriangleSideException(ss1,ss2,ss3);
System.out.println(tri.tostring());
}
catch(IlleagalTriangleException ex)
{
System.out.println(ex.toString());
}
}
}
@SuppressWarnings("serial")
class IlleagalTriangleException extends Exception{
public IlleagalTriangleException(double s1,double s2,double s3) {
super("三边不合法:三角形任意两边之和应不不大于第三边。");
}
}
class TriangleSideException{
private double side1,side2,side3;
public TriangleSideException(double side1,double side2,double side3) throws IlleagalTriangleException
{
if((side1+side2)>side3&&(side1+side3)>side2&&(side3+side2)>side1)
{
this.side1 = side1;this.side2 = side2;this.side3 = side3;
}
else
{throw new IlleagalTriangleException(side1,side2,side3);}
}
public String tostring() {
return "合法";
}
}
心得:实验过程中try块中语句块只要发生异常就立即爆出异常对象,然后被相应catch块接住,也就是try之后裔码不会在程序中执行,固然也不会有任何作用。当catch记住对象后,会执行相应catch之后裔码,而无论与否发生异常finally语句会执行,它提供统一出口。我在解决异常类时候浮现了在try中建立变量而到catch和finally中无法使用,由于在try中建立变量是局部变量,无法在catch和finally中使用。自定义异常类中Exception抛出是要注意格式(Exception e)。这次实验对于异常解决作为程序开发一种重要内容,它优势关系到程序健壮性和稳定性。也学会了自定义异常类用法。
展开阅读全文