Skip to content

CatchAndPrintStackTrace: document that only a catch block with just printStackTrace() is reported #6332

Open

Description

@MarkLee131

Check: CatchAndPrintStackTrace

Error Prone version: 2.50.0, JDK 25

Description

The description says "Logging or rethrowing exceptions should usually be preferred to catching and calling printStackTrace", and the explanation starts with "Discarding an exception after calling printStackTrace should usually be avoided."

The check only reports a catch block whose single statement is the printStackTrace() call (matchSingleStatementBlock, CatchAndPrintStackTrace.java, lines 42 to 48). CpsVariant and CpsControl still print the stack trace and drop the exception, but do something else too, so they're silent.

That's a fair way to tell "only prints" from "handles it and also prints", but the docs don't say so, and it's hard to see from outside why the second file isn't reported. Could the description mention it? I'm not asking to change the check.

Code samples

CpsControl.java:

class CpsControl {

    int last;

    void parse(String text) {
        try {
            last = Integer.parseInt(text);
        } catch (NumberFormatException e) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    e.printStackTrace();         // inside an anonymous class: not reported either
                }
            };
            r.run();
        }
    }
}

CpsSeed.java:

class CpsSeed {

    int last;

    void parse(String text) {
        try {
            last = Integer.parseInt(text);
        } catch (NumberFormatException e) {
            e.printStackTrace();                 // [CatchAndPrintStackTrace]
        }
    }
}

CpsVariant.java:

class CpsVariant {

    int last;

    void parse(String text) {
        try {
            last = Integer.parseInt(text);
        } catch (NumberFormatException e) {
            Runnable r = () -> {
                e.printStackTrace();             // the same call, behind one more statement: not reported
            };
            r.run();
        }
    }
}

Command line

javac \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
  -XDcompilePolicy=simple --should-stop=ifError=FLOW \
  -processorpath error_prone_core-2.50.0-with-dependencies.jar -Xplugin:ErrorProne \
  -d out CpsControl.java CpsSeed.java CpsVariant.java
# the --add-exports/--add-opens flags are needed for Error Prone on a modern JDK;
# run the same command on each file

Suggested change

A sentence in the explanation, for example:

Only a catch block whose single statement is the printStackTrace() call is
reported; a block that also does something else is taken to handle the
exception.

The page is generated from docs/bugpattern/CatchAndPrintStackTrace.md.

Output of the commands above:

### Error Prone 2.50.0 on javac 25.0.3  check: CatchAndPrintStackTrace
--- SEED (CpsSeed.java) -- expected: [CatchAndPrintStackTrace]
CpsSeed.java:9: warning: [CatchAndPrintStackTrace] Logging or rethrowing exceptions should usually be preferred to catching and calling printStackTrace
            e.printStackTrace();                 // [CatchAndPrintStackTrace]
    (see https://errorprone.info/bugpattern/CatchAndPrintStackTrace)
--- VARIANT (CpsVariant.java) -- the description says nothing about this case; actual: not reported
    (none)
--- CONTROL (CpsControl.java, anonymous class) -- actual: not reported
    (none)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions