转载请附上本文地址:

http://www.cnblogs.com/nextbin/p/6219677.html

本文参考:

JAVA源码

http://swiftlet.net/archives/998

http://blog.csdn.net/kingzone_2008/article/details/8535287

Exception和Error皆继承自Throwable。下面看看这3个类的源码注释,也就明白许多。

异常(Exception)分为checked异常和unchecked异常。除了RunTimeException及其子类外,所有的Exception都是checked异常。而checked异常需要被显式地捕抓或抛出,而unchecked异常则不需要。

Exception的源码注释为:

The class {@code Exception} and its subclasses are a form of {@code Throwable} that indicates conditions that a reasonable application might want to catch.

The class {@code Exception} and any subclasses that are not also subclasses of {@link RuntimeException} are checked exceptions. Checked exceptions need to be declared in a method or constructor's {@code throws} clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

错误(Error)一般不应该被捕抓。大多数的错误都是不正常的现象。尽管ThreadDeath错误是“正常”现象,它也是错误的子类,因为大多数应用都不应该捕抓。同unchecked异常一样,Error不需要被显式地捕抓或抛出。

Error的源码注释为:

An {@code Error} is a subclass of {@code Throwable} that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The {@code ThreadDeath} error, though a "normal" condition, is also a subclass of {@code Error} because most applications should not try to catch it.

A method is not required to declare in its {@code throws} clause any subclasses of {@code Error} that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

That is, {@code Error} and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

Throwable包含调用栈和异常/错误信息。同时其可以携带一个原有(cause),即另一个Throwable,由此可形成链式Throwable,保留错误的源头。保留cause原因有二,其一,上层调用者需要知道下层被调用者的错误根源,其二,上层接口定义的抛出异常可能不允许直接抛出下层被调用者所抛出的异常

Throwable的源码注释为:

The {@code Throwable} class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java {@code throw} statement. Similarly, only this class or one of its subclasses can be the argument type in a {@code catch} clause.

For the purposes of compile-time checking of exceptions, {@code Throwable} and any subclass of {@code Throwable} that is not also a subclass of either {@link RuntimeException} or {@link Error} are regarded as checked exceptions.

Instances of two subclasses, {@link java.lang.Error} and {@link java.lang.Exception}, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).

A throwable contains a snapshot of the execution stack of its thread at the time it was created. It can also contain a message string that gives more information about the error. Over time, a throwable can {@linkplain Throwable#addSuppressed suppress} other throwables from being propagated. Finally, the throwable can also contain a cause: another throwable that caused this throwable to be constructed. The recording of this causal information is referred to as the chained exception facility, as the cause can, itself, have a cause, and so on, leading to a "chain" of exceptions, each caused by another.

One reason that a throwable may have a cause is that the class that throws it is built atop a lower layered abstraction, and an operation on the upper layer fails due to a failure in the lower layer. It would be bad design to let the throwable thrown by the lower layer propagate outward, as it is generally unrelated to the abstraction provided by the upper layer. Further, doing so would tie the API of the upper layer to the details of its implementation, assuming the lower layer's exception was a checked exception. Throwing a "wrapped exception" (i.e., an exception containing a cause) allows the upper layer to communicate the details of the failure to its caller without incurring either of these shortcomings. It preserves the flexibility to change the implementation of the upper layer without changing its API (in particular, the set of exceptions thrown by its methods).

A second reason that a throwable may have a cause is that the method that throws it must conform to a general-purpose interface that does not permit the method to throw the cause directly. For example, suppose a persistent collection conforms to the {@link java.util.Collection Collection} interface, and that its persistence is implemented atop {@code java.io}. Suppose the internals of the {@code add} method can throw an {@link java.io.IOException IOException}. The implementation can communicate the details of the {@code IOException} to its caller while conforming to the {@code Collection} interface by wrapping the {@code IOException} in an appropriate unchecked exception. (The specification for the persistent collection should indicate that it is capable of throwing such exceptions.)

A cause can be associated with a throwable in two ways: via a constructor that takes the cause as an argument, or via the {@link #initCause(Throwable)} method. New throwable classes that wish to allow causes to be associated with them should provide constructors that take a cause and delegate (perhaps indirectly) to one of the {@code Throwable} constructors that takes a cause.

Because the {@code initCause} method is public, it allows a cause to be associated with any throwable, even a "legacy throwable" whose implementation predates the addition of the exception chaining mechanism to {@code Throwable}.

By convention, class {@code Throwable} and its subclasses have two constructors, one that takes no arguments and one that takes a {@code String} argument that can be used to produce a detail message. Further, those subclasses that might likely have a cause associated with them should have two more constructors, one that takes a {@code Throwable} (the cause), and one that takes a {@code String} (the detail message) and a {@code Throwable} (the cause).

转载请附上本文地址:

http://www.cnblogs.com/nextbin/p/6219677.html

本文参考:

JAVA源码

http://swiftlet.net/archives/998

http://blog.csdn.net/kingzone_2008/article/details/8535287

JAVA基础 Exception, Error的更多相关文章

  1. Java 基础 - Exception和Error

    综述 Exception 和 Error 都是继承了 Throwable 类,在 Java 中只有 Throwable 类型的实例才可以被抛出(throw)或者捕获(catch),它是异常处理机制的基 ...

  2. Java基础-异常(Exception)处理

    Java基础-异常(Exception)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.异常的概述 什么是异常?Java代码在运行时期发生的问题就是异常.在Java中,把异 ...

  3. Java异常处理总结Exception\Error

    Java异常处理总结Exception\Error 2012-12-28 08:17:17|  分类: JAVA |  标签:java  |举报|字号 订阅   Java异常处理总结          ...

  4. 2015年11月26日 Java基础系列(五)异常Exception

    序,异常都是标准类Throwable的一些子类的对象. Throwable类的几个方法 1 getMessage() 返回描述该异常的信息 2 printStackTrace() 把消息和栈的跟踪记录 ...

  5. nested exception is java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'daoSupport': ...

  6. Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í??

    2010-5-18 22:00:38 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The Apache Tomca ...

  7. Kafka中错误:Unrecognized VM option ‘UseCompressedOops’ Error: Clould not create the Java Vritual Machine. Error: A fatal exception has occurres . Program will exit.

    错误的描述: 在kafka安装目录下,执行 $ bin/zookeeper-server-start.sh config/zookeeper.properties & Unrecognized ...

  8. Java的Exception和Error面试题10问10答

    在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...

  9. java virtual machine launcher Error:Could not create the Java Virtual Machine. Error:A Fatal exception has occurred,Program will exit.

    Error:Could not create the Java Virtual Machine. Error:A Fatal exception has occurred,Program will e ...

随机推荐

  1. oracle db link的查看创建与删除

    1.查看dblink select owner,object_name from dba_objects where object_type='DATABASE LINK'; 或者 select * ...

  2. 使用ARP欺骗, 截取局域网中任意一台机器的网页请求,破解用户名密码等信息

    ARP欺骗的作用 当你在网吧玩,发现有人玩LOL大吵大闹, 用ARP欺骗把他踢下线吧 当你在咖啡厅看上某一个看书的妹纸,又不好意思开口要微信号, 用arp欺骗,不知不觉获取到她的微信号和聊天记录,吓一 ...

  3. 命令行工具aspnet_regiis.exe实现加密和解密web.config

    命令行工具aspnet_regiis.exe,是一个类似于DOS的命令工具,称之为命令解释器.使用命令行工具加密和解密web.config文件中的数据库连接字符串时,只需要简单的语法命令即可. 加密语 ...

  4. C#检验参数合法性公用方法

    #region 检验参数合法性,数值类型不小于0,引用类型不能为null,否则抛出异常 /// <summary> /// 检验参数合法性,数值类型不小于0,引用类型不能为null,否则抛 ...

  5. maven管理本地jar包注意事项

    今天lucene中集成第三方中文分词器IKAnalyzer的时候遇到了相似的问题:lucene版本4.9.IKAnalyzer版本2012FF_hf1 直接去maven仓库下载,pom配置如下: &l ...

  6. bzoj2330: [SCOI2011]糖果

    2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友 ...

  7. 【JavaScript】ArtTemplate个人的使用体验。

    据说ArtTemplate是腾讯的,感觉这东西真不错,使用方便,用起来很简单,哈哈.腾讯也不完全只是坑爹啊. ArtTemplate 使用是,正常引入js,这个自然不用说.这东西啥时候使用呢?我觉得这 ...

  8. sql 批量插入数据到Sqlserver中 效率较高的方法

    使用SqlBulk #region 方式二 static void InsertTwo() { Console.WriteLine("使用Bulk插入的实现方式"); Stopwa ...

  9. Mac安装mysql

    ### 第一步 安装后一定要记住初始密码 ### 第二步 打开终端 cd /usr/local/mysql/bin ./mysql -u root -p #输入初始密码 set password = ...

  10. 嵌入式 Linux下永久生效环境变量bashrc

    嵌入式 Linux下永久生效环境变量bashrc 1) .bashrc文件 在linux系统普通用户目录(cd /home/xxx)或root用户目录(cd /root)下,用指令ls -al可以看到 ...