JDK1.8源码笔记(8) Throwable&Exception

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.
Throwable接口是所有异常和错误的父类。只有直接或间接实现了Throwable接口的类才能被JVM或抛出语句抛出。也只有这些类才能被catch语句块捕获。

* 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.
不继承RuntimeException和Error的可抛出类是必查异常。

Simply put, if we create an exception that extends RuntimeException, it will be unchecked; otherwise, it will be checked.

              ---> Throwable <--- 
              |    (checked)     |
              |                  |
              |                  |
      ---> Exception           Error
      |    (checked)        (unchecked)
      |
RuntimeException
  (unchecked)

Oracle’s documentation tells us to use checked exceptions when we can reasonably expect the caller of our method to be able to recover.
使用必查异常就是期望这种异常的发生可以由caller妥善处理以使程序正常运行。

*

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).
Exception和Error的子类用指示异常情况的发生。

*

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.
一个throwable会显示它被创建时所在线程的execution stack。
一个throwable可以suppress别的throwable的传播。
此外,throwable还可以包括因果关系,一个throwable可以被另一个throwable创建。这被称为chained exception,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).
单纯地让底层的throwable向上传播是一个不好的设计,因为这个throwable往往和上层抽象无关。

public int getPlayerScore(String playerFile) {
    try {
        // ...
    } catch (IOException e) {
        throw new PlayerScoreException();
    }
}

public int getPlayerScore(String playerFile) {
    try {
        // ...
    } catch (IOException e) {
        throw new PlayerScoreException(e);
    }
}
The second way is better way. The point is the e.

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.

关于finally中存在return或抛出异常的补充

JDK1.8源码笔记(8) Throwable&Exception_第1张图片

https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2

你可能感兴趣的:(JDK1.8源码笔记(8) Throwable&Exception)