Overhead associated with Exception vs Throwable in Java

前端 未结 8 855
一个人的身影
一个人的身影 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:34

    You should never be throwing or catching Throwable. The scope of the exception is far too great.

    As stated previously, exceptions should be used only where needed, ie: in exceptional circumstances and should be specific to the situation that spawned them. That aside, catching a Throwable implies a host of exceptions, such as OutOfMemoryException. An error of this magnitude can not be recovered from (easily) and should not be handled by the developer.

提交回复
热议问题