Bug 219461 - [compiler][options] Limit warning for unecessary unchecked exceptions to Throwable and Exception
Summary: [compiler][options] Limit warning for unecessary unchecked exceptions to Thro...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P1 normal (vote)
Target Milestone: 3.4 M6   Edit
Assignee: Maxime Daniel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 221467
Blocks:
  Show dependency tree
 
Reported: 2008-02-19 12:00 EST by Jerome Lanneluc CLA
Modified: 2008-03-25 13:07 EDT (History)
1 user (show)

See Also:
kent_johnson: review+


Attachments
Fix - Renames option and changes semantics, aligns tests (37.58 KB, patch)
2008-03-04 02:50 EST, Maxime Daniel CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jerome Lanneluc CLA 2008-02-19 12:00:12 EST
This is a fup of bug 100278. 

In bug 100278, we went too far and we are now warning for all unchecked exceptions which is not a useful option.

We should change the option back to the original request, i.e. control whether we warn if a throws clause contains Throwable and/or Exception and no exception (checked or explicit unchecked) is actually thrown in the method body.
Comment 1 Maxime Daniel CLA 2008-03-04 02:50:44 EST
Created attachment 91483 [details]
Fix - Renames option and changes semantics, aligns tests
Comment 2 Maxime Daniel CLA 2008-03-04 04:46:24 EST
Kent, would you please let me know what you think?
Comment 3 Maxime Daniel CLA 2008-03-05 02:57:33 EST
Released for 3.4 M6.
Comment 4 Maxime Daniel CLA 2008-03-05 03:12:27 EST
In fact, JDT UI already surfaced the COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_UNCHECKED_EXCEPTIONS option. Talked to Martin, and I'll reintroduce it as deprecated, without any bearing on the compiler's behavior, for one week or so. I'll open a separate bug on JDT UI to take care of the introduction of the new option in replacement of COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_UNCHECKED_EXCEPTIONS. Must still complete the option change in M6 cycle.
Comment 5 Maxime Daniel CLA 2008-03-05 03:40:11 EST
Temporarily reintroduced COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_UNCHECKED_EXCEPTIONS as deprecated, with no effect on compiler behavior.
Opened bug 221467 against UI for the needed changes there.
Will remove COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_UNCHECKED_EXCEPTIONS as soon as possible.
Comment 6 Martin Aeschlimann CLA 2008-03-05 05:33:58 EST
Can you clarify the spec in JavaCore: That's what's currently there:

* <p>When enabled, the compiler will report neither 
*    {@link java.lang.Throwable} nor {@link java.lang.Exception} as unused 
*    declared thrown exceptions. When disabled, all declared thrown checked 
*    exceptions will be considered.

I don't understand what the second sentence means.

I thought, the normal behaviour is to never consider unchecked exceptions as usused.
With the new option, 'Throwable' and 'Exception' are looked at and a.) always considered unused or b.) only when there's no code that could possibly throw an exception (like an empty body) or c.) hen there's no code that explicitly throws a either checked or unchecked exception.
Comment 7 Maxime Daniel CLA 2008-03-05 05:45:18 EST
The fact is, Throwable and Exception are checked exceptions. The former (pre-bug 100278) behavior of the compiler exempted them because, these are the two only checked exceptions that may catch unchecked exceptions (which means that reporting that they are declared but never - explicitly - thrown somewhat overlooks the fact that they may in fact stand here for unchecked exceptions that the code may throw without showing it). What we do now (with this bug) is that we allow the user to decide that he prefers not to exempt them from the diagnostic (by moving the new COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_EXEMPT_EXCEPTION_AND_THROWABLE option from default - enabled - to disabled).
The second sentence hence says that when the option is disabled, all declared thrown checked exceptions will be considered.
Comment 8 Martin Aeschlimann CLA 2008-03-05 06:21:34 EST
Thanks for the good explanation:
Can you improve the second sentence? It would be helpful to have it describe what happens to 'Exception' and 'Throwable' when the option is disabled.

'When the option is disabled, 'Exception' and 'Throwable' will be marked unused when... '
Comment 9 Maxime Daniel CLA 2008-03-05 08:57:01 EST
Suggestion would be:

amend the doc for COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION into:
	 * <p>When enabled, the compiler will issue an error or a warning when a 
	 *    method or a constructor is declaring a checked exception as thrown,
         *    but never actually raises it in its body.
(the following lines explain that other options further tune this)

amend the doc for COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_EXEMPT_EXCEPTION_AND_THROWABLE into:
	 * <p>When enabled, the compiler will issue an error or a warning when a 
	 *    method or a constructor is declaring a checked exception as thrown,
         *    but never actually raises it in its body, except if the said
         *    exception is {@link java.lang.Throwable} or {@link 
         *    java.lang.Exception}. When disabled, the compiler will issue an 
         *    error or a warning when a method or a constructor is declaring a
         *    checked exception as thrown, but never actually raises it in its 
         *    body, including if the said exception is {@link java.lang.Throwable} or {@link 
         *    java.lang.Exception}
(the following line says that the error level is set by COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION, which is our standard way of documenting dependent options)

Please let me know what you think. I'll amend the doc when I'll get rid of the deprecated option.
Comment 10 Martin Aeschlimann CLA 2008-03-05 09:43:18 EST
I think the addition to 'COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION' is good.

For COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION, just improve the second sentence: I'm asking because I'm wondering how the compiler treats potentially thrown unchecked exceptions.

Is it:
a.) 'When the option is disabled, 'Exception' and 'Throwable' will be marked unused when no statement in the body explicitly declares to throw a checked or unchecked exception. Note that unchecked exception are not required to be declared  explicitly. However, the compiler only analyzes explicitly declared unchecked exceptions.'

or

b.) 'When the option is disabled, 'Exception' and 'Throwable' will be marked unused when no statement in the body specifies to throw a checked exception'

or

c.) 'When the option is disabled, 'Exception' and 'Throwable' will be marked unused when its guaranteed that no statement in the body can throw a checked exception or unchecked exception'
Comment 11 Maxime Daniel CLA 2008-03-05 10:03:45 EST
Would be a) semantics (except that the wording you propose is much focused on message sends, whereas throw statements will be considered as well, and catch clauses absorb some exceptions). 
Will think more about it and come up with a better proposition then, since the question you ask bears on all exceptions (what happens when an exception that extends another one is thrown?) and probes for a better doc for COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION as well.
Comment 12 Martin Aeschlimann CLA 2008-03-05 10:51:16 EST
Feature wise, is it important that we have a.)?

Or wouldn't be b.) simpler to explain and also more useful?
Comment 13 Maxime Daniel CLA 2008-03-10 06:47:58 EDT
In fact, this is not quite a). The general rule is throwing an exception that extends the one that is declared clears the warning out. It just happens that, for Throwable and Exception, there exists runtime exception that apply in these circumstances. If we want to keep the design consistent, then I believe we should not change the behavior.

I've released an improved (or at least, I hope so) documentation for the options bunch, the one for COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_EXEMPT_EXCEPTION_AND_THROWABLE now being:

	/**
	 * Compiler option ID: Reporting Unused Declared Thrown Exception Exempts Exception And Throwable.
	 * <p>When enabled, the compiler will issue an error or a warning when a 
	 *    method or a constructor is declaring a checked exception else than
	 *    {@link java.lang.Throwable} or {@link java.lang.Exception} as thrown,
	 *    but its body actually raises neither that exception, nor any other 
	 *    exception extending it. When disabled, the compiler will issue an 
	 *    error or a warning when a method or a constructor is declaring a 
	 *    checked exception (including {@link java.lang.Throwable} and 
	 *    {@link java.lang.Exception}) as thrown, but its body actually raises 
	 *    neither that exception, nor any other exception extending it. 
	 * <p>The severity of the unused declared thrown exception problem is 
	 *    controlled with option 
	 *    {@link #COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION}.
	 * <p>This diagnostic is further tuned by options
	 *    {@link #COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_DOC_COMMENT_REFERENCE}
	 *    and {@link #COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_WHEN_OVERRIDING}.
	 * <dl>
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable"</code></dd>
	 * <dt>Possible values:</dt><dd><code>{ "enabled", "disabled" }</code></dd>
	 * <dt>Default:</dt><dd><code>"enabled"</code></dd>
	 * </dl>
	 * @since 3.4
	 * @category CompilerOptionID
	 */

Please let me know when I can delete the deprecated constant.
Comment 14 Martin Aeschlimann CLA 2008-03-10 07:57:42 EDT
The spec is much better now, thanks. The complexity is hidden in the work raise. I wouldn't mind if it would at least hint at a difference between runtime and checked exceptions. Something like: 'Note that the analysis treats runtime exception like checked exceptions, and only explicitly thrown or declared runtime exception are used for the analysis'.

We must not forget to add this to the help as well, maybe even with some examples. The one sentence description on the preference page can not capture that. 

You can remove the old constant now, 
Comment 15 Maxime Daniel CLA 2008-03-10 09:22:06 EDT
You're welcome.

I think we are focusing just a bit too much on the unchecked/checked controversy now because we've been through it recently. It is difficult to strike the right balance between clarity and conciseness. What I suggest is the following: when we upgrade the help, we attempt to provide more verbose explanations and include the unchecked/checked difference discussion there. Then we could reconsider whether or not the javadoc needs further improvement.

I'll remove the deprecated constant right away.
Comment 16 Maxime Daniel CLA 2008-03-10 09:53:41 EDT
Removed COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_INCLUDE_UNCHECKED_EXCEPTIONS.

Released for 3.4 M6 (again).
Comment 17 Kent Johnson CLA 2008-03-25 13:07:34 EDT
Verified for 3.4M6 using build I20080324-1300