Throwable、RuntimeException类的使用以及java6注释处理器外文翻译资料

 2022-08-19 04:08

Throwable、RuntimeException类的使用以及java6注释处理器

1、Throwable类的使用部分

java.lang
Class Throwable

java.lang.Object

java.lang.Throwable

All Implemented Interfaces:

Serializable

Direct Known Subclasses:

Error, Exception

public class Throwable

extends Object

implements Serializable

The 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 throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause.

Instances of two subclasses, Error and 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. Finally, it can contain a cause: another throwable that caused this throwable to get thrown. The cause facility is new in release 1.4. It is also known 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 layers 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 Collection interface, and that its persistence is implemented atop java.io. Suppose the internals of the add method can throw an IOException. The implementation can communicate the details of the IOException to its caller while conforming to the Collection interface by wrapping the 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 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 Throwable constructors that takes a cause. For example:

try {

lowLevelOp();

} catch (LowLevelException le) {

throw new HighLevelException(le); // Chaining-aware constructor

}

Because the 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 Throwable. For example:

try {

lowLevelOp();

} catch (LowLevelException le) {

throw (HighLevelException)

new HighLevelException().initCause(le); // Legacy constructor

}

Prior to release 1.4, there were many throwables that had their own non-standard exception chaining mechanisms ( ExceptionInInitializerError, ClassNotFoundException, UndeclaredThrowableException, Throwable、RuntimeException类的使用以及java6注释处理器

lt;a data-cke-saved-href='mk:@MSITStore:G:苏州科技学院毕业设计外文翻译jdk api 1.6 英文版.chm::/java/lang/reflect/InvocationTargetException.htm

1、Throwable类的使用部分

java.lang
类 Throwable

java.lang.Object

java.lang.Throwable

所有已实现的接口:

Serializable

直接已知子类:

Error, Exception

public class Throwable

extends Object

implements Serializable

Throwable 类是 Java 语言中所有错误或异常的超类。只有当对象是此类(或其子类之一)的实例时,才能通过 Java 虚拟机或者 Java throw 语句抛出。类似地,只有此类或其子类之一才可以是 catch 子句中的参数类型。

两个子类的实例,ErrorException,通常用于指示发生了异常情况。通常,这些实例是在异常情况的上下文中新近创建的,因此包含了相关的信息(比如堆栈跟踪数据)。

Throwable 包含了其线程创建时线程执行堆栈的快照。它还包含了给出有关错误更多信息的消息字符串。最后,它还可以包含 cause(原因):另一个导致此 throwable 抛出的 throwable。此 cause 设施在 1.4 版本中首次出现。它也称为异常链 设施,因为 cause 自身也会有 cause,依此类推,就形成了异常链,每个异常都是由另一个异常引起的。

导致 throwable cause 的一个理由是,抛出它的类构建在低层抽象之中,而高层操作由于低层操作的失败而失败。让低层抛出的 throwable 向外传播是一种糟糕的设计方法,因为它通常与高层提供的抽象不相关。此外,这样做将高层 API 与其实现细节关联起来,假定低层异常是经过检查的异常。抛出“经过包装的异常”(即包含 cause 的异常)允许高层与其调用方交流失败详细信息,而不会招致上述任何一个缺点。这种方式保留了改变高层实现而不改变其 API 的灵活性(尤其是,异常集合通过其方法抛出)。

导致 throwable cause 的另一个 cause 是,抛出它的方法必须符合通用接口,而通用接口不允许方法直接抛出 cause。例如,假定持久集合符合 Collection 接口,而其持久性在 java.io 的基础上实现。假定 add 方法的内部可以抛出 IOException。实现可以与其调用方交流 IOException 的详细消息,同时通过以一种合适的未检查的异常来包装 IOException,使其符合 Collection 接口。(持久集合的规范应该指示它能够抛出这种异常。)

Cause 可以通过两种方式与 throwable 关联起来:通过一个将 cause 看作参数的构造方法;或者通过 initCause(Throwable) 方法。对于那些希望将 cause 与其关联起来的新 throwable 类,应该提供带有 cause 的构造方法,并委托(可能间接)给一个带有 cause 的 Throwable 构造方法。例如:

try {

lowLevelOp();

} catch (LowLevelException le) {

throw new HighLevelException(le); // Chaining-aware constructor

}

因为 initCause 方法是公共的,它允许 cause 与任何 throwable 相关联,甚至包括“遗留 throwable”,它的实现提前将异常链机制的附件应用到 Throwable。例如:

try {

lowLevelOp();

} catch (LowLevelException le) {

throw (HighLevelException)

new HighLevelException().initCause(le); // Legacy constructor

}

在版本 1.4 之前,许多 throwable 有自己的非标准异常链机制( ExceptionInInitializerErrorClassNotFoundExceptionUndeclaredThrowableExceptionInvocationTargetExceptionWriteAbortedExceptionPrivilegedActionExceptionPrinterIOExceptionRemoteExceptionNamingException)。所有这些 throwable 都已经更新过,可以使用标准异常链机制,同时继续实现其“遗留”链机制,以保持兼容性。

此外,从版本 1.4 开始,许多通用的 Throwable 类(例如,ExceptionRuntimeExceptionError)都已经更新,具有带 cause 的构造方法。由于有 initCause 方法存在,这不是严格要求的,但它更方便,也更形象地委托给一个带有 cause 的构造方法。

根据惯例,Throwable 类及其子类有两个构造方法,一个不带参数,另一个带有 String 参数,此参数可用于生成详细消息。此外,这些子类很可能有与其相关联的 cause,因此也应有两个构造方法,一个带 Throwable (cause),一个带 String(详细消息)和 Throwable (cause)。

在版本 1.4 中还引入了 getStackTrace() 方法,它允许通过各种形式的 printStackTrace() 方法编程访问堆栈跟踪信息,这些信息以前只能以文本形式使用。此信息已经添加到该类的序列化表示形式,因此 getStackTrace 和 printStackTrace 将可在反序列化时获得的 throwable 上正确操作。

从以下版本开始:

JDK1.0

另请参见:

序列化表格

构造方法摘要

Throwable()
构造一个将 null 作为其详细消息的

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[499801],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。