Skip to content

Commit a9851fe

Browse files
cpovirkError Prone Team
authored andcommitted
Document that Java uses object identity for Throwable equality in a couple places, even if you override equals.
PiperOrigin-RevId: 940126889
1 parent deb4a74 commit a9851fe

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

docs/bugpattern/ThrowableEqualsHashCode.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ Throwables should not override `equals()` or `hashCode()`.
3232
anti-pattern. Exceptions are for exceptional circumstances; they are heavy
3333
(because of the stack trace) and slow to generate.
3434

35+
4. **Java Still Sometimes Compares Exceptions by Object Identity** Even if two
36+
exceptions are "the same" according to `equals`, Java will still print both
37+
if it encounters them during a call to `printStackTrace`, and it will allow
38+
one to be a suppressed exception of the other. In both ways, two exceptions
39+
that are "the same" will continue to be treated as different by Java.
40+
3541
### **Recommended Alternative: A separate value object**
3642

3743
If you find yourself needing to compare exceptions, **extract the state into a

0 commit comments

Comments
 (0)