- 浏览: 253399 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
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.
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.
发表评论
-
各种语言写的wordcount
2015-09-24 16:07 0Java版本: String input ... -
数组双指针算法的研究
2015-07-14 16:59 2461双指针算法在数组/链 ... -
初识ThreadLocal
2015-07-07 13:15 1516最近公司在进行Java开发人员的招聘活动,其中有一道面试题 ... -
摩尔投票法
2015-06-30 20:13 18413摩尔投票法 提问: 给定一个int型数组,找出该数 ... -
小心寄存器
2012-11-08 13:53 4试试这段代码就知道了 public cla ... -
简单工作流引擎
2012-07-06 16:58 2414从公司的一个项目中挖出来的工作流引擎的代码,虽然是一个很简单的 ... -
Always clean the ThreadLocal variables.
2012-05-24 09:16 1217Any variable stored in ThreadLo ... -
STRUTS2 源码 - Logging System
2012-05-24 08:51 1405看了STRUTS2的源码,了解了它的logging系统,觉得还 ... -
在线词典的数据结构实现。
2012-05-18 08:37 0昨天在网上看到了一道百度的面试题: Baidu写道 ... -
Log4j 代码学习 - Factory
2012-05-17 08:47 1110我们最早提到,Log4j的初始代码在LogManager的静态 ... -
Log4j 代码学习 - Appender
2012-05-16 09:09 1357在上一篇文章里,我们 ... -
Log4j 代码学习
2012-05-15 14:58 1163最近闲来无事,正好手头上有Log4j的代码,于是就拿来学习了下 ... -
java7中的ThreadLocalRandom(转)
2012-01-20 09:08 4348今天早上看到一个关于java7中的ThreadLocalRan ... -
(转)追MM与23种设计模式
2011-11-16 14:13 10001、FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德 ... -
(转)Java 参数列表
2011-11-05 19:48 2926下面的讨论以Windows ... -
(转)TOMCAT源码分析
2011-10-17 16:06 2137TOMCAT源码分析(启动框架 ... -
java写的四则运算器
2011-08-19 22:19 2726本打算做一个从RE到NFA的转换器,思路已经理清了,但是在动手 ... -
MBeanServer中instantiate 和 invoke的区别
2011-06-02 11:52 1304JMX中有两种方式调用另一个MBean中的方法 先创建一个M ... -
JMX 的一个简单例子
2011-05-30 17:41 1063废话不多说,上代码: HelloWorldMBean接口 ... -
执行JAR文件的一些问题(转)
2011-03-25 13:41 1386大家都知道一个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
单项海洋环境影响评价等级表.docx
【作品名称】:基于AT89C51 单片机为核心器件,程序设计采用C 语言,Keil 软件编译程序,配以相关外围接口电路,实现了方波、锯齿波、正弦波、三角波、梯形波五种特定波形的产生【论文+源码】 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】:本设计中的波形发生器系统要求基于51单片机,因此选用以AT89C51单片机作为整个系统的控制核心,应用其强大的接口功能,构成整个波形发生器的硬件系统。使用C 语言对单片机编程可产生相应的正弦波,方波,三角波,锯齿波梯形波波形信号。在程序运行时,当接收到按键信息后,需要输出某种波形时,调用相应的中断服务子程序和波形发生程序,经电路的数/模转换器和运算放大器处理后,从信号发生器的输出端口输出即可得到要求的波形。 当需要改变频率时只需要改变单片机的波形发生程序中的递增或者递减变量即可。 【资源声明】:本资源作为“参考资料”而不是“定制需求”,代码只能作为参考,不能完全复制照搬。需要有一定的基础看懂代码,自行调试代码并解决报错,能自行添加功能修改代码。
数学建模培训资料 数学建模实战题目真题答案解析解题过程&论文报告 完全多元图的最大匹配问题研究 共9页.pdf
本项目是基于Python Web的社区爱心养老管理系统,旨在为社区养老提供一个全面、高效的管理平台。系统集成了用户管理、老人信息管理、健康管理、活动管理、服务管理等多项功能,通过简洁明了的界面,让管理人员能够轻松地进行各项操作,从而更好地服务于社区老人。 在架构上,系统采用B/S模式,前端使用HTML、CSS、JavaScript等技术,搭配Vue.js框架,实现了用户友好的交互界面;后端则基于Python的Django框架,提供了稳定且高效的服务端逻辑处理能力。数据库选用MySQL,确保了数据的存储安全和高效访问。 开发此项目的目的,不仅是为了满足计算机相关专业学生的毕设需求,提供一个实战练习的平台,更是希望通过实际项目的开发,培养学生的专业技能和实践能力,同时,也希望能为社区养老服务贡献一份力量,通过科技手段,让老年人的生活更加美好、便捷。
教学版单体spring-petlinic,课程《Kubernetes微服务实践》
内容概要:本文介绍了16世纪法国外交家Blaise de Vigenère提出的一种多表密码算法,详细解释了Vigenère密码的加密解密机制及其历史应用背景。特别提到了当明文M的长度超过密钥K的情况下,密钥会被重复使用的技巧。 适合人群:对古典密码学感兴趣的初学者,以及希望深入理解密码编码基本原理的学习者。 使用场景及目标:了解Vigenère密码的工作原理,掌握简单的加解密技术,增强信息安全意识。能够自行实施加解密操作,理解经典密码学的基本概念和技术。 其他说明:本文不仅提供了理论讲解,还给出了具体的例子帮助理解和实操练习。
STM32-EMBPI : Embedded Pi, triple-play platform for STM32, Raspberry Pi and Arduino
内容概要:本文主要探讨了电子电气架构中的网络管理策略,尤其是针对汽车中多个ECU(Electronic Control Unit)的协同管理和低功耗设计。通过引入网络管理状态机的概念,详细介绍了各状态(如常规运行状态、重复报文状态、准备睡眠模式、预睡眠模式、深度睡眠模式等)的具体运作机制及其在汽车电子系统中的重要性。文中还讨论了网络管理报文的设计与传输规则,特别是控制位向量CBV的定义,强调了网络管理在节能降耗方面的关键作用。 适用人群:具备一定汽车电子工程背景的专业人士或研究者,尤其对网络管理及低功耗设计感兴趣的工程师。 使用场景及目标:适用于汽车设计与制造企业的研发部门,帮助其优化电子控制系统,提升产品能效比,降低维护成本,提高用户体验。通过对网络管理策略的理解与应用,达到降低车载电子系统功耗的目的,进而延长电动车的续航能力和降低传统燃油车的油耗。 其他说明:文章不仅提供了理论上的阐述,还包括了具体的操作指南和技术细节,有助于从业者深入理解和实施网络管理方案。同时提醒在现代信息化社会中保持屏蔽力的重要性,鼓励读者专注于自己的发展目标,避免无效的精力分散。
英飞凌TC3XX_MCAL培训PPT
缴费综合服务系-JAVA-基于springBoot高校网上缴费综合服务系统设计与实现
Python与机器学习方向,《TensorFlow基础教程》课程仓库
【资源说明】 本科毕业设计 基于Python+Django教学资源管理系统网站详细文档+全部资料.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
内容概要:文章详细讨论了汽车行业正经历的重大变革,尤其是电动汽车(EV)和软件定义汽车(SDV)对行业的深远影响。随着技术的进步,汽车的差异化优势不再仅限于机械硬件,而是越来越多地取决于软件和服务。这一转型要求汽车制造商重新评估现有的解决方案堆栈,加大在电气化和连接性等领域的投资,以实现车型的电气化并支持可扩展性和全生命周期更新。同时,汽车的开发重点已经从机电领域转向了芯片和软件领域,强调基于云计算的协作开发方法。 适合人群:汽车行业专业人士、汽车电子工程师、技术研发人员及政策制定者。 使用场景及目标:帮助读者理解和把握汽车行业数字化转型的趋势,指导他们在电动汽车和软件定义汽车领域做出正确的技术投资和战略调整。 其他说明:本文不仅讨论了技术变革,还深入剖析了由此带来的商业和运营模式的变化,为汽车行业的未来发展方向提供了洞见。
微信课堂助手 微信小程序+PHP毕业设计 源码+数据库+论文+启动教程
新设博士后科研工作站备案申请表.xlsx
的玩具 Python 实现手套蟒蛇GloVe的玩具 Python 实现。Glove 产生单词的密集向量嵌入,其中一起出现的单词在生成的向量空间中靠得很近。虽然这会产生与word2vec (在gensim中有一个很棒的 python 实现)类似的嵌入,但方法不同GloVe 通过对语料库词共现矩阵的对数进行分解来产生嵌入。代码采用异步随机梯度下降,用Cython实现,很可能存在大量bug。安装使用 pip 从 pypi 安装pip install glove_python。OSX 用户请注意由于使用 OpenMP,glove-python 无法在 Clang 下编译。要安装它,您需要一个较新的版本gcc(例如来自 Homebrew)。应该由 接收setup.py如果没有,请打开一个问题。使用 OSX 中包含的默认 Python 发行版进行构建也不受支持请尝试 Homebrew 或 Anaconda 中的版本。用法生成嵌入分为两个步骤从语料库中创建共现矩阵,然后使用它生成嵌入。该类Corpus有助于从可交互的标记构建语料库该类Glove训练嵌入(使
消息中间件rabbitmq学习的一些代码、笔记