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

北城余情 提交于 2020-01-02 05:19:05

问题


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.


回答1:


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




回答2:


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.




回答3:


Running the FindBugs, CheckStyle or PMD on every build would slow everyone's builds down, and I imagine that is why you are looking at the Eclipse approach. Unfortunately, that may also be problematic, depending on availability (and robustness) of plugins. Plus, you'll still take a performance hit in incremental and (especially) full project builds.

Another alternative would be to set up a Hudson continuous integration server and configure it to run style checkers, coverage tools and so on, tracking the results over time using the Sonar plugin.




回答4:


As far as I can tell, it's not in the list at Window -> Preferences -> Java -> Compiler -> Errors/Warnings, and thus not possible - unless you fancy writing your own ecliple plugin.



来源:https://stackoverflow.com/questions/2666057/is-there-a-way-to-make-eclipse-report-a-general-catch-exception-e-as-an-erro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!