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

I am trying to encourage best practice not to catch common exceptions in Java code. eg:

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

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

I know that PMD chooses this, but I would prefer not to integrate it into every build environment at the moment.

+4
source share
4 answers

You can use the Checkstyle eclipse plugin to do the same. Check the "IllegalCatch" section in the documentation.

+7
source

FindBugs can report this:

REC : Exception breaks when Exception not thrown ( REC_CATCH_EXCEPTION )

This method uses a try-catch that catches Exception objects, but Exception not thrown into the try block, and RuntimeException is not explicitly caught. This is a common error pattern that says try { ... } catch (Exception e) { something } as a shorthand for finding several types of exceptions, each of which blocks locks, but this construct also accidentally catches RuntimeException , masking potential errors.

+3
source

Running FindBugs, CheckStyle, or PMD on each build will slow down the build process, and I think that’s why you are looking at the Eclipse approach. Unfortunately, this can also be problematic, depending on the availability (and reliability) of the plugins. In addition, you will still receive a performance hit in incremental and (especially) full-fledged projects.

Another alternative would be to install a Hudson continuous integration server and configure it to run step-by-step tools, coverage tools, etc., tracking the results over time using the Sonar plugin .

+3
source

As far as I can tell, it is not listed in Window -> Preferences -> Java -> Compiler -> Errors / Warnings, and therefore impossible - unless you decide to write your own ecliple plugin.

0
source

Source: https://habr.com/ru/post/1307325/


All Articles