Community
Participate
Working Groups
BETA_JAVA7 The 'Convert to a single multi-catch block' quick fix should be a bit smarter. From the selected catch clause it should go forward and backward to collect all other catch clauses that have the same block and form a continuous sequence that includes the selected clause. E.g. here, it should allow me to merge all but the first catch clause: package multicatch; import java.lang.reflect.InvocationTargetException; public class Reflect { public static void main(String[] args) { try { String.class.getConstructor().newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
(In reply to comment #0) > The 'Convert to a single multi-catch block' quick fix should be a bit smarter. > > From the selected catch clause it should go forward and backward to collect all > other catch clauses that have the same block and form a continuous sequence > that includes the selected clause. I expected someone to ask for this :) Another possible improvement - Currently the 'sameness' is computed by doing a string comparison of the source of catch blocks (the same is also used by join if statements with || quick assist), as a result when the 2 catch blocks differ only in the exception variable name the quick assist is not offered. e.g. try { String.class.getConstructor().newInstance(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalArgumentException e2) { // TODO Auto-generated catch block e2.printStackTrace(); }
(In reply to comment #1) Yeah, I saw that, but that's probably a more complex implementation than what we want to risk for 3.7.1. The SnippetFinder implements something like this for Extract Method. But another problem we should also fix is this: try { FileReader fileReader = new FileReader(""); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } The spec says: "It is a compile-time error if a disjunctive type contains two alternatives Di, Dj where Di is a subtype of Dj." => We should just remove a type if it is a subtype of another one. If we end up with just a simple one-type catch block, that's also OK.
Created attachment 205302 [details] testcase There's another annoying problem with this quick assist. It only shows up when the content of mutiple catch blocks are exactly same(apart from whitespace/tab differences). In the attached class, foo and foo2 are same, yet I see the "combine catch blocks" assist on foo2 and not on foo. Only after scratching my head for a few mins did I realise that there may be a whitespace difference. Indeed, in foo, the two catch blocks were differentiated by tabs and spaces. :(
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant. -- The automated Eclipse Genie.