Rethrow Caught Exception
ID |
kotlin.rethrow_caught_exception |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Kotlin |
Tags |
catch, exception |
Description
Reports catch blocks whose only statement is throw e where e is the caught exception
variable. A bare rethrow performs no extra work — no logging, no wrapping, no
transformation — and is indistinguishable from not catching the exception at all.
Rationale
// Bad — catch contributes nothing
try { ... } catch (e: IOException) {
throw e // FLAW
}
// OK — wraps the original exception, preserves the cause
try { ... } catch (e: IOException) {
throw IllegalStateException("ctx", e)
}
// OK — logs before re-throwing
try { ... } catch (e: IOException) {
log.warn("retrying", e)
throw e
}
The catch obscures the actual handling strategy and makes the source noisier. If handling
is intentionally deferred, leave a TODO; otherwise remove the catch.
Remediation
-
Remove the catch block — the exception will propagate naturally.
-
Add useful work before the rethrow (logging, metrics, resource cleanup).
-
Wrap the exception with a higher-level type, passing the original as the cause to preserve the stack trace.