Skip to main content

Find new exceptions thrown without the caught exception

org.openrewrite.staticanalysis.FindNewExceptionWithoutCause

Finds catch blocks that throw a newly created exception without referencing the caught exception, which discards the original exception's stack trace and message. Data flow (taint) tracking is used to establish whether the caught exception—or any value derived from it—reaches the thrown exception, so indirect references through local variables and string concatenation are not falsely reported. This mirrors PMD's PreserveStackTrace rule.

Recipe source

GitHub: FindNewExceptionWithoutCause.java, Issue Tracker, Maven Central

This recipe is available under the Moderne Source Available License.

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-static-analysis` in your build file or by running a shell command (in which case no build changes are needed):

  1. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.staticanalysis.FindNewExceptionWithoutCause")
    setExportDatatables(true)
    }

    repositories {
    mavenCentral()
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.40.0")
    }
  2. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Exceptions thrown without the caught cause

org.openrewrite.staticanalysis.table.ExceptionsWithoutCause

New exceptions thrown from a catch block that do not reference the caught exception.

Column NameDescription
Source pathThe path to the source file containing the offending throw.
Caught exception typeThe declared type of the exception caught by the enclosing catch clause.
Thrown exception typeThe type of the new exception thrown without referencing the caught exception.