Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make eclipse report a general "catch (Exception e)" as an error/warning (in java)?

I'm trying to encourage a best practice of not catching general exceptions in Java code. eg:

try {
  ...
} catch (Exception e) {  // bad!
  ...
}

Is there a way to flag this as an error/warning in Eclipse?

I know PMD picks this up, but I'd rather avoid integrating it into everyone's build environment at the moment.

like image 966
Rog Avatar asked Dec 13 '22 22:12

Rog


2 Answers

You can use Checkstyle eclipse plugin to do the same. Check 'IllegalCatch' section at documentation

like image 100
Adisesha Avatar answered Dec 25 '22 22:12

Adisesha


FindBugs can report this:

REC: Exception is caught when Exception is not thrown (REC_CATCH_EXCEPTION)

This method uses a try-catch block that catches Exception objects, but Exception is not thrown within the try block, and RuntimeException is not explicitly caught. It is a common bug pattern to say try { ... } catch (Exception e) { something } as a shorthand for catching a number of types of exception each of whose catch blocks is identical, but this construct also accidentally catches RuntimeException as well, masking potential bugs.

like image 36
polygenelubricants Avatar answered Dec 25 '22 23:12

polygenelubricants