Empty Catch Block

ID

swift.empty_catch_block

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Swift

Tags

CWE:1069, error-handling, reliability

Description

Reports a catch clause with an empty body. Silently swallowing thrown errors hides bugs and makes failures invisible to operators.

do {
    _ = try mayThrow()
} catch { }                            // FLAW

do {
    _ = try mayThrow()
} catch {
    log.error("...", error)            // OK — at least logged
}

Rationale

Swift’s structured error handling exists so that the failure case is explicit and unmissable. An empty catch block reverts to the unchecked-error model: the thrown error vanishes, leaving the program in an unknown state and operators with nothing to debug.

Remediation

Either log the error, rethrow, or actually handle it:

do {
    _ = try mayThrow()
} catch {
    logger.error("operation failed", error: error)
    // optionally: throw, return a fallback, surface to UI, etc.
}

// Or, if you really mean "discard the error":
_ = try? mayThrow()    // explicit Optional discard