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.
You can use Checkstyle eclipse plugin to do the same. Check 'IllegalCatch' section at documentation
FindBugs can report this:
REC:Exceptionis caught whenExceptionis not thrown (REC_CATCH_EXCEPTION)This method uses a
try-catchblock that catchesExceptionobjects, butExceptionis not thrown within thetryblock, andRuntimeExceptionis not explicitly caught. It is a common bug pattern to saytry { ... } 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 catchesRuntimeExceptionas well, masking potential bugs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With