我有一个RuntimeException
没有原因的(t.getCause() returns null
)。当我的代码运行t.initCause(exceptionToAttach)
时,它会给我一条IllegalStateException
消息“无法覆盖原因”。
我不会想到这是可能的。exceptionToAttach 只是一个 new RuntimeException()
,它似乎出于某种原因将其本身设置为原因。
有什么想法吗?
用一些相关代码编辑
public static void addCause(Throwable e, Throwable exceptionToAttach) {
// eff it, java won't let me do this right - causes will appear backwards sometimes (ie the rethrow will look like it came before the cause)
Throwable c = e;
while(true) {
if(c.getCause() == null) {
break;
}
//else
c = c.getCause(); // get cause here will most likely return null : ( - which means I can't do what I wanted to do
}
c.initCause(exceptionToAttach);
}