When should Throwable be used instead of new Exception?

前端 未结 12 1238
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  萌比男神i
    2020-11-28 09:20

    Generally, you would not throw or catch Throwable. In particular, JVM errors (that extend Error() ) are not meant to be caught by user code unless you are doing weird system-level work.

    Treat "Throwable" as a language artifact. The "Exception" class is named that because it is the one that is intended to be used by programmers when they want a code block to exit "exceptionally" - by not exiting normally or returning a value.

    That includes both regular error situations (by "regular" I mean as opposed to JVM errors) and places where you are using exceptions as a control mechanism.

提交回复
热议问题