R. *_*des 13 java callstack exception
如果我运行以下测试,它将失败:
public class CrazyExceptions {
private Exception exception;
@Before
public void setUp(){
exception = new Exception();
}
@Test
public void stackTraceMentionsTheLocationWhereTheExceptionWasThrown(){
String thisMethod = new Exception().getStackTrace()[0].getMethodName();
try {
throw exception;
}
catch(Exception e) {
assertEquals(thisMethod, e.getStackTrace()[0].getMethodName());
}
}
}
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
Expected :stackTraceMentionsTheLocationWhereTheExceptionWasThrown
Actual :setUp
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪只是平坦的说谎.
抛出异常时,为什么不重写堆栈跟踪?我不是Java开发者,也许我在这里遗漏了一些东西.
mha*_*ler 20
实例化异常时创建堆栈跟踪,而不是在抛出异常时创建堆栈跟踪.这是Java语言规范的指定行为
20.22.1 public Throwable()
This constructor initializes a newly created Throwable object with null as
its error message string. Also, the method fillInStackTrace (§20.22.5) is
called for this object.
....
20.22.5 public Throwable fillInStackTrace()
This method records within this Throwable object information about the
current state of the stack frames for the current thread.
Run Code Online (Sandbox Code Playgroud)
我不知道为什么他们这样做,但如果规范定义它,它至少在所有各种Java VM上都是一致的.
但是,您可以通过exception.fillInStackTrace()手动调用来刷新它.
另请注意,您应该使用Thread.currentThread().getStackTrace()而不是使用new Exception().getStackTrace()(坏样式).
tan*_*ens 10
异常的堆栈跟踪在异常的创建时填充.否则就不可能捕获异常,处理它并重新抛出它.原始的堆栈跟踪会丢失.
如果你想强制这个,你必须exception.fillInStackTrace()明确调用.
| 归档时间: |
|
| 查看次数: |
6918 次 |
| 最近记录: |