When should Throwable be used instead of new Exception?

前端 未结 12 1256
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 08:21

Given: Throwable is Exception\'s superclass.

When I read texts on writing your own \'exceptions\', I see examples of Throwable

12条回答
  •  悲哀的现实
    2020-11-28 09:12

    Oracle gives the answer in its official tutorial ("How to Throw Exceptions").

    First it explains that Throwables is a union of Error and Exception classes, which are 2 different things.

    Next, it makes clear that an Error is something extremely rare, that is almost impossible to recover from.

    When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error.

    And putting 1+1 together, it politely concludes, that (except for simple Exceptions) there is no point in catching other Throwables, since you can't do anything about them.

    Simple programs typically do not catch or throw Errors.

提交回复
热议问题