Why is Throwable.fillInStackTrace() method public? Why would someone use it?

后端 未结 6 751
不思量自难忘°
不思量自难忘° 2020-12-13 07:12

I\'m curious why is method fillInStackTrace of java.lang.Throwable public?

This method replaces original stack trace with that from the pl

6条回答
  •  囚心锁ツ
    2020-12-13 07:39

    One reason is for performance. Throwing and catching an exception is cheap; the expensive part is filling in the stack trace. If you override fillInStackTrace() to do nothing, creating an exception also becomes cheap.

    With cheap exceptions, you can use exceptions for flow control, which can make the code more readable in certain situations; you can use them when when implementing JVM languages where you need more advanced flow control, and they are useful if you are writing an actors library.

提交回复
热议问题