refactor: add IllegalArgumentException in getMostSpecificCause Method #34826
+3
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add null check to NestedExceptionUtils.getMostSpecificCause
Description
This pull request adds a defensive null check to the
getMostSpecificCause
method in theNestedExceptionUtils
class. The method now throws anIllegalArgumentException
when a null value is passed as theoriginal
parameter.Motivation
The
getMostSpecificCause
method is documented to never return null, as indicated by its JavaDoc comment: "return the most specific cause (never {@code null})". However, before this change, if a null value was passed to the method, it would attempt to callgetRootCause(null)
and then potentially return null, contradicting its contract.Changes Made
Added a null check at the beginning of the
getMostSpecificCause
method that throws anIllegalArgumentException
with the message "Original exception must not be null" when the original parameter is null.Benefits
Impact
This is a non-breaking change for code that uses the method correctly (passing non-null exceptions). It only affects code that incorrectly passes null values, which would have led to unexpected behavior anyway.
The change is minimal and focused on improving robustness without altering the method's core functionality.