Equals on Exception: Is it possible to include detailMessage

126 views
Skip to first unread message

Gerard Bosch

unread,
Nov 2, 2020, 8:21:41 AM11/2/20
to Project Lombok

Hi there,

When you implement your own exception and you add @EqualsAndHashCode(callSuper=true) on it, and then try to compare for equality of 2 instances of your own exception, the result will be false. This is because neither Exception nor Throwable define the `equals()` method, and they rely on Object.equals(), which compares the identity. So different instances are not equal in terms of Object.equals() definition.

So, is there a way to make the generated equals to include the Throwable's `detailMessage`? Say, if my own exception does not define any field, and only extends Java Exception, can Lombok generate an equals that includes the `super.getMessage()` somehow?

Thanks!
Reply all
Reply to author
Forward
0 new messages