Community
Participate
Working Groups
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.
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.
(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 { }
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
Fix contained in patch in bug 348860 comment 11. Fixed in BETA_JAVA7.
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.
Verified.