Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 350713 - [1.7][quick fix] Provide 'Add exception to existing catch clause' quick fix
Summary: [1.7][quick fix] Provide 'Add exception to existing catch clause' quick fix
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.7.1   Edit
Assignee: Deepak Azad CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-29 10:55 EDT by Deepak Azad CLA
Modified: 2011-08-02 05:45 EDT (History)
3 users (show)

See Also:


Attachments
additional fix (3.82 KB, patch)
2011-07-14 07:42 EDT, Deepak Azad CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Deepak Azad CLA 2011-06-29 10:55:38 EDT
Currently in the following snippet we provide 3 quick fixes
- Add throws declaration  
- Add catch clause to surrounding try 
- Surround with try/catch 

------------------------------------------------------------------------
        int foobar(int a) {
		try {
			if (a < 10)
				throw new FileNotFoundException(); //error
			else if (a < 20)
				throw new InterruptedIOException();
		} catch (InterruptedIOException ex) {
			ex.printStackTrace();
		}
		return a;
	}
------------------------------------------------------------------------

In 1.7 we can provide another quick fix to add the exception to the existing catch. This quick fix should be available when there is only 1 catch block.
Comment 1 Markus Keller CLA 2011-06-29 13:16:12 EDT
In some cases this will mean to replace an existing type from the catch clause, e.g. here:
		try {
			throw new IOException(); //error
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}

Not sure about what to do when the existing catch clause can't handle the additional exception (e.g. calls a method on ex or passes it to another method). Probably the best is to just be dumb and let the user deal with the follow-up problem.
Comment 2 Deepak Azad CLA 2011-07-04 05:45:09 EDT
(In reply to comment #1)
> In some cases this will mean to replace an existing type from the catch clause,
> e.g. here:
>         try {
>             throw new IOException(); //error
>         } catch (FileNotFoundException ex) {
>             ex.printStackTrace();
>         }
Markus, I think it is better to be dumb even in this situation, the user may not realize that the existing type has been replaced silently and may want to take some other action in case of a supertype subtype relationship e.g. handle both exceptions in separate catch clauses. Also Bug 348860 provides a quick-fix for the follow up problem. 

(In reply to comment #0)
We can also provide "Add multi-catch clause to surrounding try" when there is no existing catch block (try with resources or try-finally) and more than one exception is thrown. For example in the following snippet.

try {
	getClass().getDeclaredMethod("xxx").invoke(this, (Object[]) null);
} finally {		
}
Comment 3 Markus Keller CLA 2011-07-04 09:30:58 EDT
Yup, let's make the "Add exception to existing catch clause" quick fix just do what its name says (without added smartness).

> We can also provide "Add multi-catch clause to surrounding try" when there is
> no existing catch block

Yes, sounds good. So we'll have one new 1.7-only quick fix with 3 names:
- Add exception to existing catch clause
- Add exceptions to existing catch clause
- Add multi-catch clause to surrounding try
Comment 4 Deepak Azad CLA 2011-07-06 22:47:39 EDT
Fix contained in patch in bug 348860 comment 11.

Fixed in BETA_JAVA7.
Comment 5 Deepak Azad CLA 2011-07-14 07:42:50 EDT
Created attachment 199652 [details]
additional fix

(In reply to comment #3)
> Yes, sounds good. So we'll have one new 1.7-only quick fix with 3 names:
> - Add exception to existing catch clause
> - Add exceptions to existing catch clause
> - Add multi-catch clause to surrounding try

I had missed to add the 2nd string. Released in BETA_JAVA7.
Comment 6 Jay Arthanareeswaran CLA 2011-07-19 06:01:55 EDT
Verified.