Catch Variable Reassigned

ID

java.catch_variable_reassigned

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, reliability

Description

Reports assignments to the caught exception variable inside a catch block. Reassigning the catch parameter discards the original exception and its stack trace.

Rationale

When a catch block reassigns the exception variable, the original exception — including its type, message, and stack trace — is lost. This makes debugging production failures significantly harder because the root cause information is overwritten.

// Bad - original exception is overwritten
try {
    parseConfig(data);
} catch (ConfigException e) {
    e = new RuntimeException("config error");
    throw e; // original stack trace is gone
}

Remediation

Wrap the original exception as a cause instead of reassigning the variable, or declare a new variable for the replacement:

// Good - original exception preserved as cause
try {
    parseConfig(data);
} catch (ConfigException e) {
    throw new RuntimeException("config error", e);
}