JavaSE---java中的error_exception

1、Throwable

    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.
    Only this class or one of its subclasses can be the argument type in a catch clause.

      Throwable类是Java语言中所有  Error 和 Exception 的超类。

      只有Throwable 的实例 或者 Throwable 子类的实例 才能被JVM 抛出,或者通过code throw 抛出。

      只有这个类或它的一个子类可以是catch子句中的参数类型。

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

      为了对异常进行编译时检查,Throwable和Throwable的任何子类如果不是RuntimeException或Error的子类,则被视为checked exception。

    Instances of two subclasses, java.lang.Error and 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).

      两个子类java.lang.Error和java.lang.Exception的实例通常用于指示发生了异常情况。

      通常,这些实例是在异常情况的上下文中新创建的,以便包含相关信息(例如堆栈跟踪数据)。

    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 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包含 其线程创建时的执行堆栈快照。  

      它还可以包含提供有关错误的更多信息的消息字符串。

    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).

      可丢弃的对象可能有原因的一个原因是,抛出它的类构建在较低层的抽象之上,而上层的操作由于较低层的失败而失败。

      让下层抛出的可丢弃对象向外传播是不好的设计,因为它通常与上层提供的抽象无关。

      此外,这样做会将上层的API与其实现的细节联系起来,假设下层的异常是已检查的异常。

      抛出“包装异常”(即,包含原因的异常)允许上层将故障的详细信息传达给其调用方,而不会产生这些缺点。

      它保留了在不更改上层API(特别是其方法引发的异常集)的情况下更改上层实现的灵活性。

    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.)

      第二个原因是抛出它的方法必须符合通用接口,该接口不允许该方法直接抛出原因。

      例如,假设一个持久性集合符合{@link java.util.collection collection}接口,并且它的持久性是在{@code java.io}上实现的。

      假设{@code add}方法的内部可以抛出一个{@link java.io.IOException}。

      通过将{@code IOException}包装在适当的未检查异常中,实现可以将{@code IOException}的详细信息传递给调用方,同时符合{@code Collection}接口。

      (持久性集合的规范应表明它能够引发此类异常。)

 

2、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.

      Exception 和 Exception的子类  是  Throwable (应用程序希望捕获)的一种形式。

      Exception 和 Exception的子类 是 checked exceptions

      Checked exceptions 需要 被声明在一个方法 或者 构造器的throws 语句。

 

3、RuntimeException

    {@code RuntimeException} is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
    {@code RuntimeException} and its subclasses are unchecked exceptions.
    Unchecked exceptions do not 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.

      RuntimeException 是JVM运行时抛出异常的 超基类。

      RuntimeException 和 RuntimeException 的子类 是 unchecked exceptions

      Unchecked exceptions 不需要 在方法 或者 构造器中 显式声明。

 

4、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.

      {@code Error}是{@code Throwable}的一个子类,它表示一个合理的应用程序不应该试图捕捉的严重问题。

      大多数此类错误都是异常情况。

      {@code ThreadDeath}错误虽然是一种“正常”情况,但也是{@code error}的一个子类,因为大多数应用程序不应该尝试捕捉它。

      方法不需要在其{@code-throws}子句中声明{@code-Error}的任何子类,这些子类可能在方法执行期间抛出,但未被捕获,因为这些错误是不应该发生的异常情况。

      Error 和 Error的子类 被视为 unchecked exceptions

posted on   anpeiyong  阅读(79)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示