Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,15 @@ void testGCI94() {
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);
}

@Test
void testGCI1166() {
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/DontCatchRuntimeExceptions.java";
String ruleId = "creedengo-java:GCI1166";
String ruleMsg = "Don't catch RuntimeExceptions";
int[] startLines = new int[]{27, 35, 43, 50};
int[] endLines = new int[]{27, 35, 43, 50};

checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.java.checks;

import java.io.File;
import java.io.FileReader;

public class DontCatchRuntimeExceptions {
public void test1() {
try {
// some code that may throw an exception
} catch (RuntimeException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

public void test2() {
int[] array = new int[10];
try {
array[10] = 0;
} catch (IndexOutOfBoundsException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

public void test3(){
Object obj = null;
try {
obj.toString();
} catch (NullPointerException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

public void test4(){
try {
int result = 1 / 0;
} catch (ArithmeticException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

//these exceptions are ok because they aren't runtime exceptions

public void testCompliant1() {
try {
File file = new File("nonexistent.txt");
FileReader fr = new FileReader(file);
fr.read();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

// these exceptions are ok because they are IllegalArgumentExceptions, and can hardly be avoided

public void testCompliant2() {
try {
// some code that may throw an exception
} catch (IllegalArgumentException e) {
}
}

public void testCompliant3() {
try {
Integer.parseInt("abc");
} catch (NumberFormatException e) {
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class,
UseOptionalOrElseGetVsOrElse.class,
MakeNonReassignedVariablesConstants.class
MakeNonReassignedVariablesConstants.class,
DontCatchRuntimeExceptions.class
);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.java.checks;

import java.util.Collections;
import java.util.List;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.CatchTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.Tree.Kind;

@Rule(key = "GCI1166")
public class DontCatchRuntimeExceptions extends IssuableSubscriptionVisitor {

protected static final String MESSAGERULE = "Don't catch RuntimeExceptions";
protected static final String RUNTIME_EXCEPTION = "java.lang.RuntimeException";
protected static final String ILLEGAL_ARGUMENT_EXCEPTION = "java.lang.IllegalArgumentException";

@Override
public List<Kind> nodesToVisit() {
return Collections.singletonList(Kind.CATCH);
}

@Override
public void visitNode(Tree tree) {
CatchTree catchTree = (CatchTree) tree;
if(catchTree.parameter().type().symbolType().isSubtypeOf(RUNTIME_EXCEPTION) &&
!catchTree.parameter().type().symbolType().isSubtypeOf(ILLEGAL_ARGUMENT_EXCEPTION)) {
reportIssue(tree, MESSAGERULE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"GCI78",
"GCI79",
"GCI82",
"GCI94"
"GCI94",
"GCI1166"
]
}
82 changes: 82 additions & 0 deletions src/test/files/DontCatchRuntimeExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.java.checks;

import java.io.File;
import java.io.FileReader;

public class DontCatchRuntimeExceptions {
public void test1() {
try {
// some code that may throw an exception
} catch (RuntimeException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

public void test2() {
int[] array = new int[10];
try {
array[10] = 0;
} catch (IndexOutOfBoundsException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

public void test3(){
Object obj = null;
try {
obj.toString();
} catch (NullPointerException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

public void test4(){
try {
int result = 1 / 0;
} catch (ArithmeticException e) { // Noncompliant {{Don't catch RuntimeExceptions}}
}
}

//these exceptions are ok because they aren't runtime exceptions

public void testCompliant1() {
try {
File file = new File("nonexistent.txt");
FileReader fr = new FileReader(file);
fr.read();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

// these exceptions are ok because they are IllegalArgumentExceptions, and can hardly be avoided

public void testCompliant2() {
try {
// some code that may throw an exception
} catch (IllegalArgumentException e) {
}
}

public void testCompliant3() {
try {
Integer.parseInt("abc");
} catch (NumberFormatException e) {
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.java.checks;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

class DontCatchRuntimeExceptionsTest {

@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/DontCatchRuntimeExceptions.java")
.withCheck(new DontCatchRuntimeExceptions())
.verifyIssues();
}

}