实验指导书:实验7-异常处理与异常类.docx

上传人:scccc 文档编号:11282485 上传时间:2021-07-20 格式:DOCX 页数:9 大小:251.16KB
返回 下载 相关 举报
实验指导书:实验7-异常处理与异常类.docx_第1页
第1页 / 共9页
实验指导书:实验7-异常处理与异常类.docx_第2页
第2页 / 共9页
实验指导书:实验7-异常处理与异常类.docx_第3页
第3页 / 共9页
实验指导书:实验7-异常处理与异常类.docx_第4页
第4页 / 共9页
实验指导书:实验7-异常处理与异常类.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《实验指导书:实验7-异常处理与异常类.docx》由会员分享,可在线阅读,更多相关《实验指导书:实验7-异常处理与异常类.docx(9页珍藏版)》请在三一文库上搜索。

1、如果您需要使用本文档,请点击下载按钮下载!实验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* (InputMismatchE

2、xception) (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英文版八版P4

3、56: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 messa

4、ge 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.1 / 8如果您需要使用本文档,请点击下载按钮下载!2

5、.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 IllegalTriangl

6、eException 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 Ille

7、galTriangleException / Implement it三、程序清单及结果:1,package 异常处理;import java.util.Scanner;public class text1 public static void main(String args) / TODO Auto-generated method stubint a,b;Scanner s=new Scanner(System.in);System.out.println(read two integers:);while(true)try a=s.nextInt();b=s.nextInt();Sys

8、tem.out.printf(%d,a+b);catch(Exception e)System.out.println(read the number again);s.nextLine();2 / 8如果您需要使用本文档,请点击下载按钮下载!2package 异常处理;import java.util.Scanner;public class text2 public static void main(String args) / TODO Auto-generated method stubint sz=new int100;for(int i=0;isz.length;i+)szi=(i

9、nt)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(szn);catch(ArrayIndexOutOfBoundsException e)System.out.println(System.out.println);3import java.lang.Math;import java.io.BufferedReader;import java.io.Inpu

10、tStreamReader;/自定义异常类,继承与异常类Exceptionclass MyException extends ExceptionMyException(String cause,double m)/若出现异常,每处理一次异常,标记flag-shiyan3.flag-;/输出出错提示语句System.out.println(cause+的值为:+m+小于等于零);3 / 8如果您需要使用本文档,请点击下载按钮下载!class DaiKuan/定义变量贷款额、年利率、贷款年限、月还款额、总还款额double DaiKuanE,NianLiLv,DaiKuanNianShu,YueK

11、uanHuanE,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=Dou

12、ble.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(DaiKua

13、nNianShuside3&(side1+side3)side2&(side3+side2)side1)this.side1 = side1; this.side2 = side2; this.side3 = side3; elsethrow new IlleagalTriangleException(side1,side2,side3);public String tostring() return 合法;6 / 8如果您需要使用本文档,请点击下载按钮下载!7 / 8如果您需要使用本文档,请点击下载按钮下载!心得:实验过程中try块中的语句块只要发生异常就立刻爆出异常对象,然后被对应的cat

14、ch块接住,也就是try之后的代码不会在程序中执行,当然也不会有任何的作用。当catch记住对象后,会执行相应的catch之后的代码,而无论是否发生异常finally语句会执行,它提供统一的出口。我在处理异常类的时候出现了在try中建立的变量而到catch和finally中无法使用,因为在try中建立变量是局部变量,无法在catch和finally中使用。自定义异常类中Exception抛出是要注意格式(Exception e)。这次试验对于异常处理作为程序开发的一个重要内容,它的优势关系到程序的健壮性和稳定性。也学会了自定义异常类的使用方法。 (注:可编辑下载,若有不当之处,请指正,谢谢!) 8 / 8

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1