Do Not Catch java.lang.Exception

ID

java.no_catch_generic_exception

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:396, reliability

Description

Reports catch blocks that catch the generic Exception type.

Rationale

Catching Exception swallows every checked and unchecked exception, including NullPointerException, ArrayIndexOutOfBoundsException, and other programming errors. This makes error handling unreliable because genuine bugs are silently consumed along with the expected exceptions.

// Bad — catches everything, including programming errors
try {
    processFile(path);
} catch (Exception e) {
    log.error("failed", e);
}

Remediation

Catch only the specific exception types your code can meaningfully handle:

// Good — catches only what is expected
try {
    processFile(path);
} catch (IOException e) {
    log.error("file I/O failed", e);
} catch (ParseException e) {
    log.error("bad file format", e);
}