下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验题目: 异常处理一、实验目的了解程序运行中出现的各种错误,针对不同的错误,采取不同的手段排除错 误。理解异常处理机制的运行方式,掌握Java异常的抛出、捕获及处理方法,熟悉自定义异常的作用,具备发现及处理程序错误的能力,使应用程序具有稳定 性和可靠性。二、实验要求由字符串s构造日期,其中format指定s的日期格式,如“ yy年MM月dd 日”、“MM月dd日(当年)”等,默认为“ yyyy年MM月dd日”;当s用不能 转换成日期时,抛出日期格式异常 DateFormatException。public MyDate(String s,String format)throws DateFo
2、rmatException(/format指定日期格式)public static MyDate parseDate(String s,String format) throws DateFormatException三、实验原理Java语言不仅能在编译时检查出所有的语法错误,还能在运行时捕获到; Java的异常处理提供语言级对运行时错误的处理机制,是程序能够捕获并处理 运行错误。Java的异常处理机制包括异常类体系、异常处理的try语句、抛出自 定义异常对象的throw语句、声明方法抛出异常的throw子句和自定义异常类。 这些功能不仅能捕获并处理异常,还能够主动抛出异常,也能够将异常传给调
3、 用者。四、实验结果11月6日11月日17年11月06日1997年10月12日|五、心得体会通过此次实验,了解了程序运行中出现的各种错误, 针对不同的错误,采取 不同的手段排除错误。理解了异常处理机制的运行方式,掌握Java异常的抛出、 捕获及处理方法,熟悉自定义异常的作用,使应用程序具有稳定性和可靠性六、附录(把代码放在这部分)1. 声明 DateFormatException 日期格式异常类:SuppressWarnings(serial)public class DateFormatException extends IllegalArgumentExceptionpublic Date
4、FormatException(String s)super(s);public DateFormatException()super();2. 声明MyDate类: package test5_1;public class MyDate private int year,month,day;private static int thisYear;staticthisYear=2018;public MyDate(int year,int month,int day)throws DateFormatException this.set(year, month, day);public MyD
5、ate()throws DateFormatExceptionthis.set(1970,1,1);public MyDate(MyDate d)throws DateFormatExceptionthis.set(d.year,d.month,d.day);public void set(int year, int month, int day)throws DateFormatExceptionif(year2500)throw new DateFormatException(year+, 年份不合适);if(month12)throw new DateFormatException(mo
6、nth+, 月份错误 );if(dayMyDate.daysOfMonth(year, month)throw new DateFormatException(year+ 年+month+月+day+”日, 日期错误 );this.year = year;this.month=(month=1&month=1&day=31)?day:1;public MyDate(String s)throws DateFormatExceptionthis.set(Integer.parseInt(s.substring(0,4),Integer.parseInt(s.substring(5,7),Inte
7、ger.parseInt(s.substring(8,10);publicMyDateparseDate(Strings)throwsNumberFormatException,DateFormatExceptionreturn new MyDate(s);public MyDate(String s,String format)throws DateFormatExceptionyear=Integer.parseInt(s.substring(0,4);month=Integer.parseInt(s.substring(5,7);day=Integer.parseInt(s.substr
8、ing(8,10);this.set(year, month, day);format=this.toString();if(this.year=thisYear)format=String.format(%02d,month)+月+String.format(%02d,day)+ 日 ; elseif(Integer.parseInt(s.substring(0,2)=(thisYear/100)format=year%100+ 年 +String.format(%02d,month)+ 月 +String.format(%02d,day)+ 日 ;System.out.println(fo
9、rmat);public MyDate parseDate(String s,String format)throws DateFormatExceptionreturn new MyDate(s,format);public int getYear()return this.year;public int getMonth() return this.month;public int getDay() return this.day;public String toString() return year+ 年 +String.format(%02d,month)+ 月 +String.fo
10、rmat(%02d,day)+ 日 ;public static int getThisYear() return thisYear;public static boolean isLeapYear(int year) return year%400=0|year%100!=0&year%4=0;public boolean isLeapYear() return isLeapYear(this.year);public static int daysOfMonth(int year,int month)switch(month)case 1:case 3:case 5:case 7:case
11、 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;case 2:return MyDate.isLeapYear(year)?29:28;default:return 0;public int daysOfMonth() return daysOfMonth(this.year,this.month);3. 主函数:package test5_1;public class MyDate_exargs)throwspublic staticvoid main(StringNumberFormatException,DateFormatExceptionString s1=2018/11/06;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 云南省昆明市西山区民中2026届物理高一第一学期期末质量跟踪监视试题含解析
- 排水板低温弯折性试验记录
- 土的液限(碟式仪法)试验记录
- 医院关于进一步规范各类讲座、论坛、报告会、研讨会审批流程的通知
- 高一数学上学期第一次月考(北师大版2019高效培优-强化卷)(全解全析)
- 浅谈《聊斋志异》中的花妖
- 建筑工程管理中供应链管理的关键问题探讨
- 语言学类论文英语标题信息结构的对比研究
- 略论朱熹的经典诠释思想
- 论文的格式字体要求
- 体验营销课件
- 色谱分析教案及反思总结
- GB/T 5762-2024建材用石灰石、生石灰和熟石灰化学分析方法
- 输水涵洞改造工程施工方案
- (高清版)DZT 0388-2021 矿区地下水监测规范
- 医共体工作总结汇报
- 《上海主流生活剖析》课件
- AI工业质检行业现状分析报告
- 热稳定校验计算书
- 美团闪购行业分析
- 牛顿第一定律的实验验证
评论
0/150
提交评论