




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验实验 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 DaiKuanNianShuside3 this side2 side2 this side3 side3 else throw new IlleagalTriangleException side1 side2 side3 public String tostring return 合法 心得 实验过程中try块中的语句块只要发生异常就立刻爆出异常对象 然后被 对应的catch块接住 也就是try之后的代码不会在程序中执行 当然也不会有任 何的作用 当catch记住对象后 会执行相应的catch之后的代码 而无论是否发 生异常fin
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025江苏盐城市东台市教育局直属学校招聘教师、教练员58人考前自测高频考点模拟试题及答案详解1套
- 2025年福建省盲人协会招聘1人模拟试卷及一套参考答案详解
- 中国钴胺素行业市场运行态势及发展趋势预测报告-智研咨询发布
- 宾阳中考模拟考试题目及答案
- 2025年福建省柘荣法院招聘1人考前自测高频考点模拟试题附答案详解
- 智研咨询发布:中国低空消防行业市场现状、发展概况、未来前景分析报告
- 疼痛平分课件
- 2025广西南宁市良庆区玉洞街道办事处社区公益性岗位劳动保障协管员招聘1人模拟试卷附答案详解
- 2025年总局遴选面试真题及答案
- 化工原料失重率检验规程集
- 直流电压和电流的测量
- 中学教职工参加升旗仪式的规范要求
- 2023年成都轨道交通集团有限公司招聘笔试题库及答案解析
- GB/T 15820-1995聚乙烯压力管材与管件连接的耐拉拔试验
- GB 4706.76-2008家用和类似用途电器的安全灭虫器的特殊要求
- 女性生殖系统炎症病人的护理课件
- 部编人教版九年级语文上册第14课《故乡》课件
- 全国人大代表候选人初步建议人选登记表
- 禾川x3系列伺服说明书
- 诗歌《舟夜书所见》课件
- (完整版)历年中医药大学博士考题
评论
0/150
提交评论