Is there any reason to establish the reason for the exception to myself?

I came across some places in java libraries that I create against where the cause of the exception is set for the exception itself.

Is there a reason the exception is referenced as a reason?

EDIT

As requested, here is a specific example:

enter image description here

+5
source share
4 answers

No, it's just a bad design. If the exception is the root cause, he does not need to determine the root cause.

, , . , , PersistenceExcpetion. , , IOException. , , SqlException. Etc

+2

, , Hibernate Spring, ( GUI ).

, , . , JSON: bam, .

, :

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 , , .

, , initCause , cause this.

: Exception ( , ), , cause, initCause, !

, .

+8

, . ...

, Throwable . , initCause(), , , IllegalArgumentException.

, , Throwable.

, , ( ), , getCause() Throwable , null, == this. , , , Throwable , .

+8

From the sources ThrowableI have:

public synchronized Throwable initCause(Throwable cause) {
    ...
    if (cause == this)
        throw new IllegalArgumentException("Self-causation not permitted");
    ...
}

I do not see how the setting itself can be itself.

+3
source

All Articles