Empty Try Block
ID |
kotlin.empty_try_block |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Kotlin |
Tags |
CWE:1071, empty, reliability |
Description
Reports try blocks whose body contains no statements.
A try block exists to wrap code that may throw an exception so that a catch or finally
clause can handle the outcome. When the body is empty there is nothing to protect: any
attached catch and finally blocks are unreachable dead code. Such constructs almost
always result from an incomplete implementation.
Rationale
// Bad — try body is empty, nothing is protected
try { } catch (e: Exception) { handle(e) } // FLAW
// OK — try body contains code to protect
try {
riskyOperation()
} catch (e: Exception) {
handle(e)
}
Remediation
-
Add the code that should be protected inside the
tryblock. -
If no code needs protection, remove the entire
try / catchconstruct.