Why can I “fake” the stack trace of an exception in Java?

后端 未结 6 829
梦如初夏
梦如初夏 2021-02-19 10:39

If I run the following test, it fails:

public class CrazyExceptions {
    private Exception exception;

    @Before
    public void setUp(){
        exception =          


        
6条回答
  •  北荒
    北荒 (楼主)
    2021-02-19 11:17

    The stacktrace of the exception is filled in at creation time of the exception. Otherwise it would be impossible to catch an exception, handle it and rethrow it. The original stacktrace would get lost.

    If you want to force this you have to call exception.fillInStackTrace() explicitly.

提交回复
热议问题