Search code examples
javaexceptiondebugging

Why do Java Exception instances sometimes point to themselves, the debugger suggesting that causes are endless?


While debugging with Eclipse IDE an HttpClientErrorException, I noticed that the cause property contains a reference to the exception itself, so I continued and the property "cause" was there again, again and again… forever.

Why is cause's value a reference to itself?

Screenshot of Eclipse debugger showing the problem


Solution

  • How

    Throwable declares:

    private Throwable cause = this;
    

    If the cause is not initialized, either by passing a cause in the constructor or by calling initCause, it will continue to point to this. Note that consequently getCause is implemented as:

    public synchronized Throwable getCause() {
        return (cause==this ? null : cause);
    }
    

    Why

    The reason for this design is explained in Throwable's source:

    To allow Throwable objects to be made immutable and safely reused by the JVM, such as OutOfMemoryErrors, fields of Throwable that are writable in response to user actions, cause, stackTrace, and suppressedExceptions obey the following protocol:

    1. The fields are initialized to a non-null sentinel value which indicates the value has logically not been set.

    2. Writing a null to the field indicates further writes are forbidden

    3. The sentinel value may be replaced with another non-null value.

    For example, implementations of the HotSpot JVM have preallocated OutOfMemoryError objects to provide for better diagnosability of that situation. These objects are created without calling the constructor for that class and the fields in question are initialized to null. To support this capability, any new fields added to Throwable that require being initialized to a non-null value require a coordinated JVM change.