- 1、本文档共44页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* 程序举例 public class MissingValueException extends Exception { private Student student; public MissingValueException(Student s, String message) { super(message); student = s; } public Student getStudent() { return student; } } 定义异常类,此类存储在 MissingValueException.java文件中。 * 程序举例 public class Student { private String name; private String ssn; public String getSsn() { ... } public void setSsn() { ... } public String getName() { ... } public void setName(String n) throws MissingValueException { if (n.equals()) { throw new MissingValueException(this, A students name cannot be blank); } else { name = n; } } // etc. } 使异常类在Student类中运作。 * 程序举例 public class Example { public static void main(String[] args) { String name = read value from GUI; Student s = new Student(); s.setName(name); // etc. 在试图调用setName方法的那行代码上将产生编译错误: Unreported exception MissingValueException; must be caught or declared to be thrown s.setName(name); 因为在Student类的setName方法声明中包含了throws MissingValueException子句,java编译器强制客户代码捕获这个类型的异常。 伪代码 * 程序举例 public class Example { public static void main(String[] args) { String name = read value from GUI; Student s = new Student(); … try { s.setName(name); } catch (MissingValueException e) { System.out.println(e.getMessage()); System.out.println(ID of affected student: + e.getStudent().getSsn()); } // etc. 输出结果: A students name cannot be blank ID of affected student: 123-45-6789 伪代码 此处假设在调用setName之前已经为ssn属性赋了初值。 补充: 枚举类型 * 枚举类型 将变量的取值约束到一个有限的集合。 例如:只希望学位的取值为: Mathematics Biology Chemistry Computer Science Physical Education 为保证客户代码不会传入一个不正确的专业值,因此创造一个自定义异常类型InvalidMajorExcetion,代码如下所示: JDK 1.5 特征 * public class Student { private String name; private String major; public Student(String name, String major) throws InvalidMajorException { this.setName(name); if (major not one of the five approved majors) { throw new InvalidMajorException(); } else { this.setMajor(major); } } …… } // 在运行时,若传入错误的参数,则会丢出异常 try { Student s = new Student(Dorothy Jost, Cul
您可能关注的文档
- 第6章数据域测试概述重点.ppt
- 第6章数模和模数转换重点.ppt
- 第11章文字的使用重点.ppt
- 第9章输入输出系统重点.ppt
- 水泵与水泵站3--运行10学时重点.ppt
- 第6章贪心算法重点.ppt
- 第11章信息隐藏与数字水印重点.ppt
- 水厂工程关键施工技术重点.doc
- 语文熊猫路6年级剖析.doc
- 水池满水试验施工方案重点.doc
- 【产业图谱】2024年四川低空经济产业全景图谱(附产业布局、发展现状、重点企业等).docx
- 银行间市场NPAS指数2024年第4季度.pdf
- AI视觉检测解决方案.docx
- 2024年中国晶泰科技企业研究:AI变革行业创新发展 -头豹.docx
- 儿童牙膏:宝贝的护齿伙伴,健康与趣味同行的成长之路 头豹词条报告系列.docx
- 【易凯】2024年GLP-1赛道大事记.docx
- 三七内参(2025年第5期 总119期).pdf
- 营销策划 -盒你·更新美好 -企业定制礼盒项目创意简报.docx
- deepseek 与 ChatGPT 的比较介绍.docx
- 先进制造2025观察第10期:全球民用无人机进出口情况观察.docx
最近下载
- 每周工作4小时—蒂莫里.费里斯.pdf
- 2024年苏州工业职业技术学院高职单招(英语/数学/语文)笔试历年参考题库含答案解析.docx
- 陕旅版四年级下册英语教案完整版(最全).doc
- 北师大版2024-2025学年一年级数学下册教学工作计划(及进度表).docx
- 2024年湖南科技职业学院高职单招职业技能测验历年参考题库(频考版)含答案解析.docx
- 动画分镜设计.ppt VIP
- 2024年苏州工业职业技术学院单招职业技能测试题库及答案解析.docx
- 大学四级英语单词.doc VIP
- FUNAC发那科 机器人系统高级编程Karel中文版.pdf
- 雨课堂学堂在线《计算机网络(湖北科技学院)》学堂云单元测试考核答案.pdf
文档评论(0)