《实验指导书:实验7-异常处理与异常类(共8页).docx》由会员分享,可在线阅读,更多相关《实验指导书:实验7-异常处理与异常类(共8页).docx(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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* (InputMismatchException
2、) (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*
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 o
4、f 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、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 c
6、onstructor 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 i
7、t三、程序清单及结果: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();System.out.printf(%d,a+b);catch(Excep
8、tion e)System.out.println(read the number again);s.nextLine();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=(int)Math.random()*100;System.out.println(enter the index of t
9、he 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.InputStreamReader;/自定义异常类,继承与异常类Exceptionclass MyException exten
10、ds ExceptionMyException(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/依次输入贷款额、年利率
11、、贷款年限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(E
12、xception 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(DaiKuanNianShuside3&(side1+side3)side2&(side3+side2)side1)this.side1 = side1; this.side2 = s
13、ide2; this.side3 = side3; elsethrow new IlleagalTriangleException(side1,side2,side3);public String tostring() return 合法;心得:实验过程中try块中的语句块只要发生异常就立刻爆出异常对象,然后被对应的catch块接住,也就是try之后的代码不会在程序中执行,当然也不会有任何的作用。当catch记住对象后,会执行相应的catch之后的代码,而无论是否发生异常finally语句会执行,它提供统一的出口。我在处理异常类的时候出现了在try中建立的变量而到catch和finally中无法使用,因为在try中建立变量是局部变量,无法在catch和finally中使用。自定义异常类中Exception抛出是要注意格式(Exception e)。这次试验对于异常处理作为程序开发的一个重要内容,它的优势关系到程序的健壮性和稳定性。也学会了自定义异常类的使用方法。专心-专注-专业