Skip to content

[DeadCode] Skip dead catch before catch-all Throwable/Exception in RemoveDeadCatchRector - #8462

Merged
TomasVotruba merged 2 commits into
mainfrom
fix-9888-dead-catch-before-catch-all
Sep 4, 2026
Merged

[DeadCode] Skip dead catch before catch-all Throwable/Exception in RemoveDeadCatchRector#8462
TomasVotruba merged 2 commits into
mainfrom
fix-9888-dead-catch-before-catch-all

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Sep 4, 2026

Copy link
Copy Markdown
Member

Fixes #9888

A catch block that only re-throws its own exception is not dead when a later catch of Throwable or Exception handles the same exception differently. Removing it would let the exception fall through to the catch-all block and change behavior.

Because Throwable/Exception catch anything, the current catch type often cannot be resolved to prove the parent relation (e.g. a third-party ValidationException unknown to reflection). The rule now treats those two types as catch-all explicitly, so the preceding re-throw catch is kept.

Before (wrongly removed):

try {
    // ...
} catch (ValidationException $e) {
    throw $e;
} catch (\Exception $e) {
    echo 'general';
}

Added skip_dead_catch_before_catch_all_exception fixture covering the case.

https://claude.ai/code/session_011TpTyRop7qReoHY1MfLpqF

…moveDeadCatchRector

A catch block that only re-throws its own exception is not dead when a
later catch of Throwable or Exception handles it differently. As those
types catch anything, the current catch type cannot always be resolved to
prove the parent relation, so treat them as catch-all explicitly.

Fixes #9888

Claude-Session: https://claude.ai/code/session_011TpTyRop7qReoHY1MfLpqF
…emoveDeadCatchRector

An unresolvable next catch type may be a parent of the current caught
exception, so removing the re-throwing catch could change behavior by
routing the exception to a different handler.

Claude-Session: https://claude.ai/code/session_01Bh57AAY3DpLKXGX5c7EETM
@TomasVotruba
TomasVotruba merged commit bf6552a into main Sep 4, 2026
43 checks passed
@TomasVotruba
TomasVotruba deleted the fix-9888-dead-catch-before-catch-all branch September 4, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

RemoveDeadCatchRector should not remove dead catch block if multiple catch block exist and dead catch exception is child of any other caught exception

1 participant