This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 229479 - "exception is too broad" hint: add option to only check JDK 7 + (or make the hint JDK 7 +)
Summary: "exception is too broad" hint: add option to only check JDK 7 + (or make the ...
Status: VERIFIED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.4
Hardware: PC Mac OS X
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-05-07 20:12 UTC by athompson
Modified: 2013-05-22 11:56 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description athompson 2013-05-07 20:12:11 UTC
While the reasoning behind the "exception is too broad" hint is valid, in many people's judgement the more concise code produced by the anti-pattern of catching an overly broad exception outweighs doing things the "proper" way and repeating code in each catch block. Multicatch (and its more concise code) negates the need for this anti-pattern (thus the warning is desirable again where it's available), but it's only available in JDK 7+.
Comment 1 athompson 2013-05-14 07:45:26 UTC
In no case should there be a warning if the exception is wrapped and re-thrown. For example:

try {
    ...
} catch(Exception e) {
    throw new RuntimeException("error", e);
}
Comment 2 Svata Dedic 2013-05-14 09:41:35 UTC
(In reply to comment #0)
> While the reasoning behind the "exception is too broad" hint is valid, in many
> people's judgement the more concise code produced by the anti-pattern of
> catching an overly broad exception outweighs doing things the "proper" way and
> repeating code in each catch block. Multicatch (and its more concise code)

It is the purpose of the hint to alert of a *possible* issue. I can make the hint more complex, but still the IDE cannot reliably determine whether the reason to catch 'broad' exception is valid and conforms to the team's coding style or requirements on that particular place.

I agree that JDK 7+ syntax decreases the need for the anti-pattern, but IMHO the hint does a valid job even in < JDK 7, in that it warns before possible unintended effects when catching broad exception type subtree - we used successfully similar hint in IntelliJ IDEA in my past projects to pinpoint places which are worth human inspection.

Personally I find the anti-pattern more harmful as it leads to suprises in exception handling as the code in the guarded block changes and starts to throw additional exceptions - espectiall with 'catch all' clauses like 
catch(Exception ex) {}, or 
catch (Exception ex) { 
  if (ex instanceof Foo) { ...} 
  else if (ex instanceof Bar) { ... }
}
Replicating is not that bad, you need not to replicate more complex code can be factored out to an error handling method.

When the place is determined NOT to be harmful, it may be annotated with @SuppressWarnings() - I'll add a relevant code for this hint; sorry I forget to define one.

As with all hints - if the hint generates too many false positives, according to your opinion, or your favourite coding style, you may turn the hint off. With per-project hint settings, you may even turn the hint off selectively for projects that use JDK < 7.


(In reply to comment #1)
> In no case should there be a warning if the exception is wrapped and re-thrown.
Following the reasoning of the previous comment, the bad effect of this snippet is that leads the maintaining programmer to not care about proper error handling of even checked exceptions and it proved to be a source of errors (error handling errors :)) during code reviews I did in the past. It's better to check such places during code review to ensure proper error handling and that's why they are reported.

BTW one should never throw RuntimeException in production code. An application/feature specific subclass should be created, so the ill application state can be distinguished from programming errors (NPE, or UnsupportedOperationExc are also RuntimeExcs), but that was probably only meant as an example :)

There are many ways how to deal with a catched exception; and the proper one can be only determined by the programmer, depending on contract of the surrounding code:
throw new XXXException(original);
LOG.log(LogLevel.INFO, original);
Exceptions.printStackTrace(original);
....
Comment 3 Svata Dedic 2013-05-14 10:45:24 UTC
SupressWarnings hints added in http://hg.netbeans.org/jet-main/rev/a1e1c809a479
Comment 4 athompson 2013-05-14 12:31:33 UTC
Good idea; I forgot about the @SurpressWarnings option. IMO there still should not be a warning if the exception is re-thrown, but your call.
Comment 5 athompson 2013-05-14 13:24:46 UTC
And now that I think about it @SurpressWarnings only is fine; once the per-project hints are added for Maven I can handle the legacy code that way.
Comment 6 Quality Engineering 2013-05-19 02:52:53 UTC
Integrated into 'main-golden', will be available in build *201305182300* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/a1e1c809a479
User: Svata Dedic <sdedic@netbeans.org>
Log: #229479: Added @SuppressWarning codes
Comment 7 athompson 2013-05-22 11:56:14 UTC
yup.

BTW, are this hint and the "use specify catch" hint similar enough that they can be combined?
Comment 8 athompson 2013-05-22 11:56:46 UTC
...the use *specific catch hint.