gpt4 book ai didi

java - Throwable getCause 返回 null

转载 作者:搜寻专家 更新时间:2023-11-01 02:25:55 27 4
gpt4 key购买 nike

我遇到了一个 Throwable 错误。

catch (Throwable t){    
System.out.println(t.getCause().getMessage());
}

当我在 System.out. 行上放置一个断点,并将鼠标悬停在 (Eclipse) t 变量上时,Eclipse 会显示原因:java .lang.NoClassDefFoundError: 我的类

但是当我释放断点时,我得到 null for t.getCause()

为什么会这样?我怎样才能得到原因字符串。

更新:

如果我抓到也会发生同样的事情

catch (NoClassDefFoundError e){
}

最佳答案

答案在 docs - Throwable#getCause 中:

Returns the cause of this throwable or null if the cause is nonexistent or unknown. (The cause is the throwable that caused this throwable to get thrown.)

所以当你这样做的时候:

t.getCause().getMessage()

这就像写作:

null.getMessage()

这当然会导致 NullPointerException .

你可以简单地做:

catch (NoClassDefFoundError e){
System.out.println(e.getMessage);
}

关于java - Throwable getCause 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com