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

后端 未结 6 765
不思量自难忘°
不思量自难忘° 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:45

    One possibly legitimate use is creating an exception in a different place from where you are actually throwing. For instance, maybe you have an application where you provide a plugin facility for generating custom exceptions. Since the stacktrace is filled in on construction, the trace of the exception will include possibly misleading information (it will include calls into the custom exception factory). thus, your application would call fillInStackTrace() on the exception after generation by the custom factory to give a more accurate stack trace to the eventual receiver of the exception.

    This rejected bug indicates that you are not the only one confused by the need for this method.

提交回复
热议问题