我在我正在构建的 Java 库中遇到过一些地方,在这些地方异常的原因被设置为异常本身。
异常是否有任何理由将自身作为其原因?
编辑
根据要求,这里有一个具体的例子:
最佳答案
我经常看到 Hibernate 或 Spring 等框架或库抛出的异常将自身作为原因(混淆了过程中的调试器 GUI)。
我一直想知道他们为什么这样做,因为这似乎是个坏主意。今天,当我尝试将一个序列化为 JSON 时,它实际上导致了一个问题:bam,enless cycle。
所以我进一步调查了一下:
在 Throwable
的源代码中(这里列出的所有源代码都来自 JDK 1.7)我们有这个:
/**
* The throwable that caused this throwable to get thrown, or null if this
* throwable was not caused by another throwable, or if the causative
* throwable is unknown. If this field is equal to this throwable itself,
* it indicates that the cause of this throwable has not yet been
* initialized.
*
* @serial
* @since 1.4
*/
private Throwable cause = this;
现在我特别遇到了扩展 RuntimeException
的异常类的问题,所以我从那里开始。 RuntimeException
的构造函数之一:
/** Constructs a new runtime exception with the specified detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public RuntimeException(String message) {
super(message);
}
上面调用的Exception
的构造函数:
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public Exception(String message) {
super(message);
}
上面调用的Throwable
的构造函数:
/**
* Constructs a new throwable with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* <p>The {@link #fillInStackTrace()} method is called to initialize
* the stack trace data in the newly created throwable.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public Throwable(String message) {
fillInStackTrace();
detailMessage = message;
}
fillInStackTrace
是一种 native 方法,似乎不会修改 cause 字段。
如您所见,除非随后调用 initCause
方法,否则 cause
字段永远不会更改 this
的原始值.
结论:如果您使用不采用 cause
参数,并且您不调用 initCause
方法,异常的原因将是它本身!
所以我想这应该使它成为一种非常普遍的现象。
关于java - 是否有任何理由将异常的原因设置为自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9233638/