- 浏览: 255607 次
- 性别:
- 来自: 南京
-
文章分类
最新评论
-
mabusyao:
漠北空城 写道请问下,你这个是JDK版本是多少呢?!忘记了,应 ...
HashMap 源码解读 -
漠北空城:
请问下,你这个是JDK版本是多少呢?!
HashMap 源码解读 -
schumee:
完美团队~
项目沉思录 - 1.1 -
winie:
整理下 搞成引擎嘛 国产需要这样的engine
简单工作流引擎 -
mabusyao:
某位同学给我提供的堪称完美的解决方案:1. 将三个int数组放 ...
CraneWork
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.
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.
![](http://dl.iteye.com/upload/attachment/217347/b0702cb7-b7fa-365b-a397-ea5a7708b083.gif)
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.
![](http://dl.iteye.com/upload/attachment/217349/6335821b-2b7b-3867-9fb3-2fbf1a7d2bae.gif)
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.
![](http://dl.iteye.com/upload/attachment/217356/26b1f6b9-30a3-3bfa-8182-ca7d1a932b8d.gif)
发表评论
-
各种语言写的wordcount
2015-09-24 16:07 0Java版本: String input ... -
数组双指针算法的研究
2015-07-14 16:59 2472双指针算法在数组/链 ... -
初识ThreadLocal
2015-07-07 13:15 1527最近公司在进行Java开发人员的招聘活动,其中有一道面试题 ... -
摩尔投票法
2015-06-30 20:13 18441摩尔投票法 提问: 给定一个int型数组,找出该数 ... -
小心寄存器
2012-11-08 13:53 4试试这段代码就知道了 public cla ... -
简单工作流引擎
2012-07-06 16:58 2422从公司的一个项目中挖出来的工作流引擎的代码,虽然是一个很简单的 ... -
Always clean the ThreadLocal variables.
2012-05-24 09:16 1225Any variable stored in ThreadLo ... -
STRUTS2 源码 - Logging System
2012-05-24 08:51 1415看了STRUTS2的源码,了解了它的logging系统,觉得还 ... -
在线词典的数据结构实现。
2012-05-18 08:37 0昨天在网上看到了一道百度的面试题: Baidu写道 ... -
Log4j 代码学习 - Factory
2012-05-17 08:47 1120我们最早提到,Log4j的初始代码在LogManager的静态 ... -
Log4j 代码学习 - Appender
2012-05-16 09:09 1374在上一篇文章里,我们 ... -
Log4j 代码学习
2012-05-15 14:58 1174最近闲来无事,正好手头上有Log4j的代码,于是就拿来学习了下 ... -
java7中的ThreadLocalRandom(转)
2012-01-20 09:08 4368今天早上看到一个关于java7中的ThreadLocalRan ... -
(转)追MM与23种设计模式
2011-11-16 14:13 10081、FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德 ... -
(转)Java 参数列表
2011-11-05 19:48 2958下面的讨论以Windows ... -
(转)TOMCAT源码分析
2011-10-17 16:06 2161TOMCAT源码分析(启动框架 ... -
java写的四则运算器
2011-08-19 22:19 2753本打算做一个从RE到NFA的转换器,思路已经理清了,但是在动手 ... -
MBeanServer中instantiate 和 invoke的区别
2011-06-02 11:52 1336JMX中有两种方式调用另一个MBean中的方法 先创建一个M ... -
JMX 的一个简单例子
2011-05-30 17:41 1094废话不多说,上代码: HelloWorldMBean接口 ... -
执行JAR文件的一些问题(转)
2011-03-25 13:41 1397大家都知道一个java应用项目可以打包成一个jar,当然你必须 ...
相关推荐
Java用户注册登录模块 这是一个综合性知识要求较强的程序设计,涵盖了CoreJava的很多知识点。希望可以为读者作为参考。 主要技术点: 1.OOP 2.正则表达式 3.List集合 4.GUI(awt+swing) ...6.Excetion ……
问题描述 做项目用到了MUI的scroll控件 故此我引入了mui.min.js 然而 猝不及防地报错了: 原创文章 236获赞 36访问量 201万+ 关注 私信 展开阅读全文 作者:Piconjo_Official
法国Embouchure地区沉积物岩性数据集 内容 该数据集由O. Puertas在2014年发布,详细记录了法国Embouchure地区的EMBOU岩芯沉积物的岩性特征。数据集中包含了39个数据点,提供了关于该地区沉积环境的重要信息。欲了解更多信息,请访问"PANGAEA数据库中的原始数据" ()。
BH1750光照强度传感器
基于多目标粒子群算法的分布式电源选址定容与优化配置研究——基于MATLAB的程序实践与优化探讨,基于多目标粒子群算法的分布式电源选址定容与优化配置MATLAB程序:含规划模型、算法求解及参考文章赠阅,分布式电源选址定容与优化配置MATLAB程序基于多目标粒子群算法 (1)该程序为基于多目标粒子群算法的分布式电源优化配置与选址定容程序,期刊lunwen源程序,配有该lunwen。 (2)本程序可有效配置分布式电源容量与安装位置。程序与lunwen包含的内容有综合成本、网损、电压稳定裕度为目标函数建立分布式电源的规划模型、多目标粒子群算法、IEEE-69节点的算例求解。 (3)赠送若干极为相似的参考lunwen,均为本人研究该课题期间认为非常系统、全面、易懂、基础的文章。 ,1. 多目标粒子群算法; 2. 分布式电源优化配置; 3. 分布式电源选址定容; 4. MATLAB程序; 5. 综合成本; 6. 网损; 7. 电压稳定裕度; 8. IEEE-69节点; 9. 参考lunwen(或相关文章),基于多目标粒子群算法的分布式电源选址定容与优化配置MATLAB程序:成本、网损与电压稳定的综
sediment core GeoB17620-1的文档记录 内容 Lucchi, RG; Sabbatini, A; Nicolaisen, LS 等(2014年)提供的sediment core GeoB17620-1的文档记录。详细信息请访问"此链接" ()以获取完整的数据集文档和相关信息。请注意,文档的具体大小未知。
该项目是一款多语言心理咨询网站前端开发设计源码,包含7166个文件,涵盖2646个PHP文件、1419个JavaScript文件、650个GIF文件、417个HTML文件、318个PNG文件、263个LESS文件、236个Vue文件、219个JSON文件、183个Markdown文件、142个文本文件。主要采用JavaScript进行开发,同时融入PHP、TypeScript、Shell等多种语言技术。这套源码旨在提供全面的心理咨询服务网站界面设计。
显微镜下染色体图像分割系统:新手入门宝典
物品识别与分类系统源码和数据集:改进yolo11-HWD
【C#】Grpc扩展是一个基于GRPC的简单微服务框架
【GO】基于go实现的生日提醒定时任务
深海沉积物岩芯GeoB17624-3的文档记录 内容 Lucchi, RG; Sabbatini, A; Nicolaisen, LS 等(2014)提供的文档包括了对深海沉积物岩芯GeoB17624-3的研究资料。该研究的具体数据集大小未知,但已通过PANGAEA数据库发布,可供进一步学术研究和参考。可通过以下链接访问完整数据集:"" ()。
外科工具实例分割系统源码和数据集:改进yolo11-convnextv2
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
AI人脸识别学习,使用opencv2
内容概要:本通知介绍了西安邮电大学将举办的2024年第29届“21世纪杯”全国大学生英语演讲比赛的具体情况。活动目的是提升学生讲好中国故事的能力,促进文化交流与构建人类命运共同体。此次活动的主题是传统文化的复兴。主要内容涵盖比赛的背景介绍、参赛对象限定以及具体报名方式及时间节点。此外还详细讲解了演讲主题及视频上传的要求。评分标准从内容质量、语言表述、整体表现等方面进行了明确规定。获奖学生不仅将得到相应的表彰奖励,还有可能代表西安邮电大学参与到更高层级的比赛当中去。 适合人群:全体在校本科生,尤其是对外语学习感兴趣的师生;以及对于传统文化复兴有兴趣的年轻人。 使用场景及目标:此活动鼓励学生积极参与英语演讲竞技,提高自己的英语表达能力和综合素质,同时也有助于推广中国传统优秀文化传播。 其他说明:研究生(青年组)需要直接通过官网报名并且不受本次比赛限制;此外,该次比赛的成绩还将影响其他英语演讲系列竞赛的表现评估体系。未尽事宜将在日后进一步发布。
该项目是一款采用Java编写的自然管理后台设计源码,包含共计126个文件,涵盖95个Java源代码文件、18个XML配置文件、5个YAML文件、4个Git忽略文件、4个属性文件以及其他类型文件。此系统专注于后台管理功能,适用于各种管理场景。
太阳能光伏面板分割系统:基础知识铺垫
光伏混合储能直流微网中基于PI控制的直流母线电压下垂控制模型研究与应用,光伏混合储能直流微网中基于PI控制的直流母线电压下垂控制模型研究与应用,光伏混合储能直流微网直流母线电压下垂控制控制模型 在基于储能单元SOC的下垂控制方法基础上,下垂系数不断变化,通过加入PI二次控制,将控制器输出量与储能单元变器输出电压和母线电压稳定值相叠加,对母线电压的跌落和和上升进行补偿和抑制。 效果明显 也可以改mppt控制方法或者改局部遮阴 ,光伏混合储能、直流微网、下垂控制、PI二次控制; SOC下垂控制方法; 电压稳定性提升; MPPT控制方法调整。,光伏混合储能直流微网电压控制模型:动态下垂系数与PI二次控制