Unreachable Catch
ID |
kotlin.unreachable_catch |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Kotlin |
Tags |
dead-code, exception |
Description
Reports a later catch clause whose exception type is a subtype of an earlier
catch clause on the same try. Catch clauses are tested top-to-bottom; once
a thrown exception matches a clause it is never tested against later clauses,
so a later clause catching a subtype of one already covered above is dead code.
Rationale
// Bad
try {
work()
} catch (e: RuntimeException) {
log(e)
} catch (e: IllegalStateException) { // FLAW — already covered above
log(e)
}
// Good — order specific to general
try {
work()
} catch (e: IllegalStateException) {
log(e)
} catch (e: RuntimeException) {
log(e)
}
The rule uses a conservative, hard-coded map of well-known JDK exception subtype relationships. It does not attempt full type resolution, so it stays silent on third-party or user-defined exception hierarchies — false positives are not worth the noise.
Remediation
Reorder the catch clauses from most-specific to most-general, or delete the
unreachable clause entirely if its body duplicates the earlier handler.