`
mabusyao
  • 浏览: 254765 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

java excetion 结构

    博客分类:
  • java
阅读更多
Exceptions are the customary way in Java to indicate to a calling method that an abnormal condition has occurred. This article is a companion piece to this month's Design Techniques installment, which discusses how to use exceptions appropriately in your programs and designs. Look to this companion article for a tutorial on the nuts and bolts of what exceptions are and how they work in the Java language and virtual machine.

When a method encounters an abnormal condition (an exception condition) that it can't handle itself, it may throw an exception. Throwing an exception is like throwing a beeping, flashing red ball to indicate there is a problem that can't be handled where it occurred. Somewhere, you hope, this ball will be caught and the problem will be dealt with. Exceptions are caught by handlers positioned along the thread's method invocation stack. If the calling method isn't prepared to catch the exception, it throws the exception up to its calling method, and so on. If one of the threads of your program throws an exception that isn't caught by any method along the method invocation stack, that thread will expire. When you program in Java, you must position catchers (the exception handlers) strategically, so your program will catch and handle all exceptions from which you want your program to recover.

Exception classes
In Java, exceptions are objects. When you throw an exception, you throw an object. You can't throw just any object as an exception, however -- only those objects whose classes descend from Throwable. Throwable serves as the base class for an entire family of classes, declared in java.lang, that your program can instantiate and throw. A small part of this family is shown in Figure 1.

As you can see in Figure 1, Throwable has two direct subclasses, Exception and Error. Exceptions (members of the Exception family) are thrown to signal abnormal conditions that can often be handled by some catcher, though it's possible they may not be caught and therefore could result in a dead thread. Errors (members of the Error family) are usually thrown for more serious problems, such as OutOfMemoryError, that may not be so easy to handle. In general, code you write should throw only exceptions, not errors. Errors are usually thrown by the methods of the Java API, or by the Java virtual machine itself.


In addition to throwing objects whose classes are declared in java.lang, you can throw objects of your own design. To create your own class of throwable objects, you need only declare it as a subclass of some member of the Throwable family. In general, however, the throwable classes you define should extend class Exception. They should be "exceptions." The reasoning behind this rule will be explained later in this article.

Whether you use an existing exception class from java.lang or create one of your own depends upon the situation. In some cases, a class from java.lang will do just fine. For example, if one of your methods is invoked with an invalid argument, you could throw IllegalArgumentException, a subclass of RuntimeException in java.lang.

Other times, however, you will want to convey more information about the abnormal condition than a class from java.lang will allow. Usually, the class of the exception object itself indicates the type of abnormal condition that was encountered. For example, if a thrown exception object has class IllegalArgumentException, that indicates someone passed an illegal argument to a method. Sometimes you will want to indicate that a method encountered an abnormal condition that isn't represented by a class in the Throwable family of java.lang.

As an example, imagine you are writing a Java program that simulates a customer of a virtual café drinking a cup of coffee. Consider the exceptional conditions that might occur while the customer sips. The class hierarchy of exceptions shown in Figure 2 represents a few possibilities.



If the customer discovers, with dismay, that the coffee is cold, your program could throw a TooColdException. On the other hand, if the customer discovers that the coffee is overly hot, your program could throw a TooHotException. These conditions could be exceptions because they are (hopefully) not the normal situation in your café. (Exceptional conditions are not necessarily rare, just outside the normal flow of events.) The code for your new exception classes might look like this:

// In Source Packet in file except/ex1/TemperatureException.java
class TemperatureException extends Exception {
}

// In Source Packet in file except/ex1/TooColdException.java
class TooColdException extends TemperatureException {
}

// In Source Packet in file except/ex1/TooHotException.java
class TooHotException extends TemperatureException {
}

This family of classes, the TemperatureException family, declares three new types of exceptions for your program to throw. Note that each exception indicates by its class the kind of abnormal condition that would cause it to be thrown: TemperatureException indicates some kind of problem with temperature; TooColdException indicates something was too cold; and TooHotException indicates something was too hot. Note also that TemperatureException extends Exception -- not Throwable, Error, or any other class declared in java.lang.


Checked vs. unchecked exceptions
There are two kinds of exceptions in Java, checked and unchecked, and only checked exceptions need appear in throws clauses. The general rule is: Any checked exceptions that may be thrown in a method must either be caught or declared in the method's throws clause. Checked exceptions are so called because both the Java compiler and the Java virtual machine check to make sure this rule is obeyed.

Whether or not an exception is "checked" is determined by its position in the hierarchy of throwable classes. Figure 4 shows that some parts of the Throwable family tree contain checked exceptions while other parts contain unchecked exceptions. To create a new checked exception, you simply extend another checked exception. All throwables that are subclasses of Exception, but not subclasses of RuntimeException are checked exceptions.


  • 大小: 10.2 KB
  • 大小: 5.2 KB
  • 大小: 11.1 KB
分享到:
评论

相关推荐

    Java用户注册登录模块

    Java用户注册登录模块 这是一个综合性知识要求较强的程序设计,涵盖了CoreJava的很多知识点。希望可以为读者作为参考。 主要技术点: 1.OOP 2.正则表达式 3.List集合 4.GUI(awt+swing) ...6.Excetion ……

    Babel7.x完美解决严格模式报错:’caller’, ‘callee’, and ‘arguments’ properties may not be accessed on strict mode

    问题描述 做项目用到了MUI的scroll控件 故此我引入了mui.min.js 然而 猝不及防地报错了: 原创文章 236获赞 36访问量 201万+ 关注 私信 展开阅读全文 作者:Piconjo_Official

    小红薯电商实操课小红书开店实操必学课.mp4

    小红薯电商实操课小红书开店实操必学课.mp4

    AI图像处理工具包-一键抠图、背景切换、旧照片修复、人像漫画化、视频卡通化(Python+OpenCV+Dlib+TensorFlow).zip

    AI图像处理工具包-一键抠图、背景切换、旧照片修复、人像漫画化、视频卡通化(Python+OpenCV+Dlib+TensorFlow).zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!

    基于java的新能源充电系统设计与实现.docx

    基于java的新能源充电系统设计与实现.docx

    吸波材料建模单元周期仿真模拟,参数优化,计算反射损耗,极化角,入射角,等效阻抗等

    吸波材料建模单元周期仿真模拟,参数优化,计算反射损耗,极化角,入射角,等效阻抗等

    AGV调度系统的仿真平台(含源码+项目说明+实验结果分析).zip

    AGV调度系统的仿真平台(含源码+项目说明+实验结果分析).zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!

    【本体】Internet Download Manager v6.42.26.zip

    【本体】Internet Download Manager v6.42.26.zip

    NLP中文垃圾短信分类系统源码+设计全部资料+文档报告(自然语言处理课设).zip

    NLP中文垃圾短信分类系统源码+设计全部资料+文档报告(自然语言处理课设).zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!

    Python大作业封面.doc

    Python大作业封面.doc

    基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)

    基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业),个人经导师指导并认可通过的毕业设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在做毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开发的安卓的记事本app项目源码(高分期末大作业)基于Android Studio开

    操作系统课后习题参考答案

    操作系统课后题参考答案

    三维几何图形绘制工具软件-几何图霸

    几何图霸软件是一个非常强大的三维几何图形绘制工具,可以根据约束条件绘制标准示意图,写相关技术类文章需要绘图时很适用。

    《写给大众的健康饮食指南》.mp4

    《写给大众的健康饮食指南》.mp4

    智能相册Piktures v2.19 build 815 for 高级版.mp4

    智能相册Piktures v2.19 build 815 for 高级版.mp4

    建行开养老金必中58元微信立减金亲测.mp4

    建行开养老金必中58元微信立减金亲测.mp4

    Converter视频音频转换器v2.2.5.2解锁VIP版.mp4

    Converter视频音频转换器v2.2.5.2解锁VIP版.mp4

    车来了v4.59.0高级版 精准实时公交地铁神器.mp4

    车来了v4.59.0高级版 精准实时公交地铁神器.mp4

Global site tag (gtag.js) - Google Analytics