欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOCX文档下载  

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

    • 资源ID:11282485       资源大小:251.16KB        全文页数:9页
    • 资源格式: DOCX        下载积分:4
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录   微博登录  
    二维码
    微信扫一扫登录
    下载资源需要4
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

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

    如果您需要使用本文档,请点击下载按钮下载!实验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.1 / 8如果您需要使用本文档,请点击下载按钮下载!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 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(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;i<sz.length;i+)szi=(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(szn);catch(ArrayIndexOutOfBoundsException e)System.out.println("System.out.println");3import java.lang.Math;import java.io.BufferedReader;import java.io.InputStreamReader;/自定义异常类,继承与异常类Exceptionclass MyException extends ExceptionMyException(String cause,double m)/若出现异常,每处理一次异常,标记flag-shiyan3.flag-;/输出出错提示语句System.out.println(cause+"的值为:"+m+"小于等于零");3 / 8如果您需要使用本文档,请点击下载按钮下载!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)finally4 / 8如果您需要使用本文档,请点击下载按钮下载!/若贷款额、年利率、贷款年限均大于零,则不会抛出一次异常,故flag的值不变,仍然等于初值3if(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();4package 异常处理;import java.util.Scanner;public class text4 public static void main(String args) 5 / 8如果您需要使用本文档,请点击下载按钮下载!/ TODO Auto-generated method stubScanner 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();tryTriangleSideException 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; elsethrow new IlleagalTriangleException(side1,side2,side3);public String tostring() return "合法"6 / 8如果您需要使用本文档,请点击下载按钮下载!7 / 8如果您需要使用本文档,请点击下载按钮下载!心得:实验过程中try块中的语句块只要发生异常就立刻爆出异常对象,然后被对应的catch块接住,也就是try之后的代码不会在程序中执行,当然也不会有任何的作用。当catch记住对象后,会执行相应的catch之后的代码,而无论是否发生异常finally语句会执行,它提供统一的出口。我在处理异常类的时候出现了在try中建立的变量而到catch和finally中无法使用,因为在try中建立变量是局部变量,无法在catch和finally中使用。自定义异常类中Exception抛出是要注意格式(Exception e)。这次试验对于异常处理作为程序开发的一个重要内容,它的优势关系到程序的健壮性和稳定性。也学会了自定义异常类的使用方法。 (注:可编辑下载,若有不当之处,请指正,谢谢!) 8 / 8

    注意事项

    本文(实验指导书:实验7-异常处理与异常类.docx)为本站会员(scccc)主动上传,三一文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




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

    三一文库
    收起
    展开