From e4f4619b74420086d67395209df7d84d420793e9 Mon Sep 17 00:00:00 2001 From: Tomasz Tylenda Date: Wed, 4 Feb 2026 15:21:40 +0100 Subject: [PATCH 1/2] SONARJAVA-6095 S1166 should not report when exception uses unnamed variable --- .../main/java/checks/CatchUsesExceptionWithContextCheck.java | 3 +++ .../sonar/java/checks/CatchUsesExceptionWithContextCheck.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/java-checks-test-sources/default/src/main/java/checks/CatchUsesExceptionWithContextCheck.java b/java-checks-test-sources/default/src/main/java/checks/CatchUsesExceptionWithContextCheck.java index f35ce10abc0..ceaa44893f9 100644 --- a/java-checks-test-sources/default/src/main/java/checks/CatchUsesExceptionWithContextCheck.java +++ b/java-checks-test-sources/default/src/main/java/checks/CatchUsesExceptionWithContextCheck.java @@ -65,6 +65,9 @@ private void f(Exception x) { System.out.println("" + e); } } + try { + } catch (Exception _) { // Compliant + } } private void g() { diff --git a/java-checks/src/main/java/org/sonar/java/checks/CatchUsesExceptionWithContextCheck.java b/java-checks/src/main/java/org/sonar/java/checks/CatchUsesExceptionWithContextCheck.java index 88c031030a0..65e994d0e9a 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/CatchUsesExceptionWithContextCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/CatchUsesExceptionWithContextCheck.java @@ -170,7 +170,7 @@ public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) { @Override public void visitCatch(CatchTree tree) { - if (!isExcludedType(tree.parameter().type()) && !excludedCatchTrees.contains(tree)) { + if (!isExcludedType(tree.parameter().type()) && !excludedCatchTrees.contains(tree) && !tree.parameter().simpleName().isUnnamedVariable()) { Symbol exception = tree.parameter().symbol(); usageStatusStack.addFirst(new UsageStatus(exception.usages())); super.visitCatch(tree); From 058eb63657b97ea94e4690f491c0192e90286aca Mon Sep 17 00:00:00 2001 From: Tomasz Tylenda Date: Thu, 5 Feb 2026 11:48:41 +0100 Subject: [PATCH 2/2] Update rspec --- .../org/sonar/l10n/java/rules/java/S1166.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1166.html b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1166.html index 817abcb5bd2..ce600dff0f9 100644 --- a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1166.html +++ b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1166.html @@ -68,6 +68,16 @@

Exceptions

LOGGER.warn(message); // Compliant - exception message logged with some contextual information } +

Additionally, no issue will be raised if the exception parameter uses the unnamed variable _, which is available starting with Java +21. The unnamed variable makes it explicit that the exception is caught but not needed in the handler (see {rule:java:S7467}).

+
+try {
+  /* ... */
+} catch (Exception _) {
+  // Compliant - unnamed exception parameter is intentionally unused
+  return defaultValue;
+}
+

Resources