Bug 100278 - [compiler] Add compiler warning for explicitly declared runtime exceptions
Summary: [compiler] Add compiler warning for explicitly declared runtime exceptions
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.4 M5   Edit
Assignee: Maxime Daniel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 157155
  Show dependency tree
 
Reported: 2005-06-15 15:02 EDT by Igor Fedorenko CLA
Modified: 2008-02-04 09:27 EST (History)
4 users (show)

See Also:
kent_johnson: review+


Attachments
Fix + test cases (34.62 KB, patch)
2008-01-15 02:42 EST, Maxime Daniel CLA
no flags Details | Diff
Proposed fix (without additional tests) (1.90 KB, patch)
2008-01-23 06:54 EST, Dani Megert CLA
no flags Details | Diff
Proposed fix (without additional tests) (1.90 KB, patch)
2008-01-23 08:26 EST, Dani Megert CLA
no flags Details | Diff
Proposed fix (without additional tests) (1.88 KB, patch)
2008-01-23 08:27 EST, Dani Megert CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Igor Fedorenko CLA 2005-06-15 15:02:46 EDT
Consider the following two methods

	public void test() throws FileNotFoundException, IOException {
		throwsIOException();
	}
	public void anotherTest() throws Exception {
	}

There is no "unnecessary declaration of thrown exception" warnings as of Eclipse
3.1RC2 for these two methods. 

I can kinda understand rationale why throws FileNotFoundException is not
considered redundant but throws Exception does not make any sense to me and
makes this feature pretty much useless.
Comment 1 Olivier Thomann CLA 2005-06-15 20:12:27 EDT
There is no warning for Exception because this type can potentially catch an
unchecked exception. Same treatment for Throwable.
I hope that this clarifies the way this warning works.
Did you notice any change in the way we handle this warning between RC1 and RC2?
Comment 2 Igor Fedorenko CLA 2005-06-15 23:09:27 EDT
Thank you for the quick reply, Olivier, but I think you missed my point.

The only reason a developer would care about unnecessary "throws" declarations
is because such exceptions have to be dealt with by every caller of the method.
The callers' code will either become polluted with unnecessary catch blocks, or
it will further propagate declaration of these unnecessary exceptions. In either
case, the codebase grows with the code which not only redundant but is also a
likely source of programming errors. Throws Exception/Throwable is especially
bad since it forces the callers to (implicitly) catch RuntimeException/Error.

In other words, the goal of this warning is to help limit number of exception
types that *have* to be caught by the callers, not number of exception types
that *can* possibly be caught by the callers.

Hope this makes sense...
Comment 3 Olivier Thomann CLA 2005-06-16 08:49:47 EDT
Philippe, any comment?
Comment 4 Philipe Mulet CLA 2005-06-16 09:01:03 EDT
The debate is legite, and I could agree with it.
I think all we need to know at this stage (3.1RC3) is whether RC2 changed
behavior for you.

If not, then I would defer any change in this area. Not that it isn't a valid
concern, but not critical at this stage.
Comment 5 Igor Fedorenko CLA 2005-06-16 09:55:47 EDT
Indeed, the behaviour did not change. That only makes me wonder how I missed
this before. And I agree this should wait until after 3.1.
Comment 6 Philipe Mulet CLA 2006-09-13 07:41:46 EDT
We could consider a suboption to not consider unchecked exception as being thrown by default. This option would be off by default (keep current behavior by default).
Comment 7 Philipe Mulet CLA 2006-12-05 04:31:28 EST
removing target
Comment 8 Maxime Daniel CLA 2007-09-04 04:39:51 EDT
*** Bug 132420 has been marked as a duplicate of this bug. ***
Comment 9 Maxime Daniel CLA 2007-09-04 04:44:14 EDT
Changing the title to reflect the discussion. May have to consider both unchecked exceptions and exceptions that unchecked exceptions extend (with separate messages, but same tuning devices). Also need to discuss whether we warn upon unchecked exceptions that are explicitly declared, or only upon unchecked exceptions that are explicitly declared *and not thrown* (which was the point of bug 132420).
Comment 10 Chris Hubick CLA 2007-09-04 17:59:45 EDT
I *often* declare throws clauses for exceptions not thrown by an API method when I anticipate a subclass may need to throw such an exception, or when I anticipate some future version of the code may need to throw such an exception (ie, I may hard code a method return value today which I eventually may want to read from a database [possibly causing an exception]).  If I specifically declare such an exception, I think the default behavior should be for the compiler to assume I know what I am doing.

What I was asking for in the bug which was (I think incorrectly) marked as a dup of this, is that if I specifically declare a method as throwing an *unchecked* exception, that callers not specifically catching/handling that runtime exception should issue a warning.
Comment 11 Maxime Daniel CLA 2007-09-05 02:59:32 EDT
I see. Reopening bug 132420, with a slightly different title. Please check it matches your thoughts.
Comment 12 Maxime Daniel CLA 2008-01-15 02:42:38 EST
Created attachment 86913 [details]
Fix + test cases

Adds an option to include all exceptions in the diagnostic, disabled by default.
Comment 13 Maxime Daniel CLA 2008-01-15 02:43:10 EST
Kent, would you please let me know what you think?
Comment 14 Maxime Daniel CLA 2008-01-23 01:34:31 EST
Released for 3.4 M5.
Comment 15 Maxime Daniel CLA 2008-01-23 02:08:41 EST
Opened bug 216233 for UI consideration of the new option.
Comment 16 Dani Megert CLA 2008-01-23 06:46:35 EST
I have to reopen as this does not work:
1. the JavaCore constant value has two typos and hence doesn't match the
   option (string) defined in the internal CompilerOptions
2. test case seems to be broken or not test the public API as used by clients
3. not sure about this one but it seems that the option is not initialized for 
   batch compiler(org.eclipse.jdt.internal.compiler.batch.Main.configure(String[])
Comment 17 Dani Megert CLA 2008-01-23 06:54:09 EST
Created attachment 87631 [details]
Proposed fix (without additional tests)
Comment 18 Dani Megert CLA 2008-01-23 08:26:05 EST
Created attachment 87638 [details]
Proposed fix (without additional tests)
Comment 19 Dani Megert CLA 2008-01-23 08:27:56 EST
Created attachment 87639 [details]
Proposed fix (without additional tests)

Sorry about the previous patches. I got tricked by the wizard.
Comment 20 Maxime Daniel CLA 2008-01-23 09:08:14 EST
Thanks for the patch. 

The change to Main is not strictly needed, and given the fact that the code comments what it is doing to some length:
// override defaults: references that are embedded in javadoc are ignored
// from the perspective of parameters and thrown exceptions usage
I won't adopt it, but the other part is more than welcome. I released it along with a test variant that uses the API constant. I'll open a separate bug to suggest that we factorize constants or else test that they are consistent (I think I can see why we do not factorize them... but the mistake I made calls for some safeguards at least).

Released (again) for 3.4 M5.
Comment 21 Eric Jodet CLA 2008-02-04 09:27:17 EST
Verified for 3.4M5 using build I20080204-0010