Overhead associated with Exception vs Throwable in Java

前端 未结 8 857
一个人的身影
一个人的身影 2020-12-05 06:53

I know

throw new Exception();

has a pretty large overhead, since it creates a full stackTrace, etc.
Does

throw new Thr         


        
8条回答
  •  不知归路
    2020-12-05 07:32

    You can look at the source code of the two classes to see that Exception doesn't do anything beyond expose the same constructors as Throwable. All of the meat, and hence overhead, lives in Throwable.

    Even if Exception did introduce some additional overhead it would be a clear over-optimization to use Throwable instead. Use the right tool for the job, don't co-opt the wrong tool just because it's lighter.

提交回复
热议问题