Java Logging in Try/Catch Blocks: SonarQube Enforcement

📝 Log Error Message in Java try/catch Blocks What Copilot Missed and What SonarQube Enforces? One thing I noticed is the piece of Java code generated by Copilot using Claude didn't meet SonarQube standard because it omitted logging inside catch statements when try/catch was used. Plus exception message is also required to be included in the log. Another thing I noticed is: 🔹 SonarQube in the pipeline enforces this rule strictly 🔹 Local SonarQube (or IDE plugins) does not Here're two use cases we can either log it or do not log it: ✔ Handle the exception → MUST LOG IT try { performAction(); } catch (IOException e) { logger.warn("perform action failed: {}", e.message()); fallback(); } ❌ Do not handle the exception → DO NOT LOG IT (Just rethrow it) try { performAction(); } catch (IOException e) { throw new MyIOException("Unable to perform action", e); // No log here } This prevents double logging. #Java #SpringBoot #SonarQube #CleanCode #SoftwareEngineering #Logging #GitHubCopilot #CodeQuality #BestPractices #DevOps #AI

To view or add a comment, sign in

Explore content categories