When should Throwable be used instead of new Exception?

前端 未结 12 1230
没有蜡笔的小新
没有蜡笔的小新 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:02

    You shouldn't use Exceptions as a "return type" either...

    If the condition you're throwing for is common, you are spending a huge amount of resources to return that condition to the calling routine. Exceptions are expensive to construct.

    I've seen cases where tight loops that threw exceptions as a "negative" for e.g. ID allocation, this routine occupied about 99% of the CPU time .. when changed to a documented return constant instead, this dropped to 25%.

提交回复
热议问题