Reputation: 132
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?
Upvotes: 6
Views: 425
Reputation: 33000
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);
}
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:
The fields are initialized to a non-null sentinel value which indicates the value has logically not been set.
Writing a null to the field indicates further writes are forbidden
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.
Upvotes: 11
Reputation: 28761
Do you have access to source where this Exception
was created?
Looks like the HttpClientErrorException
object was created and then its cause
field was modified to be the same object, perhaps using initCause
.
Upvotes: 0