This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 117530

Summary: [60cat] Generated try, catch code is greedy
Product: java Reporter: rationalpi <rationalpi>
Component: HintsAssignee: Jan Lahoda <jlahoda>
Status: RESOLVED WONTFIX    
Severity: blocker    
Priority: P4    
Version: 6.x   
Hardware: PC   
OS: Windows XP   
Issue Type: DEFECT Exception Reporter:

Description rationalpi 2007-10-02 22:02:13 UTC
[ BUILD # : 200709141330 ]
[ JDK VERSION : 1.6.0_02 ]

Given a case where one is calling a method that throws an exception
and the IDE suggests generating a try, catch block as a quick fix,
the generated try, catch block is greedy and surrounds way more code
that is necessary with the try, catch block.

Steps to Reproduce
------------------
1) Create a class with the following source.
import java.io.IOException;

public class GreedyTryCatch {
    
    public static void main(String[] args) {
        System.out.println("a");
        System.out.println("b");
        System.out.println("c");
        doSomething();
        System.out.println("d");
        System.out.println("e");
        System.out.println("f");
    }
    
    public static void doSomething() throws IOException {
        throw new IOException();
    }

}
2) Click the editor hint regarding the unreported exception that must
be caught or thrown and select the "Surround with try-catch" option.
The main method will now look like this:
    public static void main(String[] args) {
        try {
            System.out.println("a");
            System.out.println("b");
            System.out.println("c");
            doSomething();
            System.out.println("d");
            System.out.println("e");
            System.out.println("f");
        } catch (IOException ex) {
           
Logger.getLogger(GreedyTryCatch.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }

Note how the try catch surrounds all of the println statements and
not just the call to the doSomething method. This isn't a big deal in
this small example, but when you have a larger block of code and
multiple levels of loops and nesting, it makes the quick fix
virtually unusable.

The easy solution, in my opinion would be to only surround the code
that is required to be surrounded with the try, catch block.

The better solution, in my opinion, is to do the easy solution by
default, but to check whether the developer has highlighted a block
of code, and if so, surround whatever block he has highlighted with
the try, catch block.
Comment 1 Jan Lahoda 2007-10-08 15:35:30 UTC
Well, please note that there are two surround with try-catch hints:
-one, bound to the error
-another, selection based: if you select a piece of code (the caret needs to be on line without error, currently), a
lightbulb suggesting to wrap the selection with try-catch should appear.
Comment 2 rationalpi 2007-10-09 14:55:51 UTC
Excellent. I like the selection based "Surround with....". I wasn't aware that was there. I think I'll use that a lot.

Is there a way that the selection based "Surround With Try {... could ALSO be mapped to the error. That would exactly
what I'm asking for.

Thanks.
Comment 3 David Strupl 2009-03-31 15:57:06 UTC
Resolving all issues with milestone "future" as LATER. If you feel strongly that
it should be implemented please reopen and set the target milestone to "next".
Comment 4 Quality Engineering 2009-11-02 10:56:36 UTC
NetBeans.org Migration: changing resolution from LATER to WONTFIX