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 197884 - Convert to try-with-resources won't use existing resourceless try block
Summary: Convert to try-with-resources won't use existing resourceless try block
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.0
Hardware: PC Windows 7 x64
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-20 04:42 UTC by MauveCloud
Modified: 2015-04-12 04:25 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description MauveCloud 2011-04-20 04:42:32 UTC
I made this small snippet to demonstrate this problem:
public static void main(final String[] args) {
    final char[] buffer = new char[100];
    try {
        final FileReader reader = new FileReader("test.txt");
        reader.read(buffer);
        reader.close();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

When I use "Convert to try-with-resources", I get:
public static void main(final String[] args) {
    final char[] buffer = new char[100];
    try {
        try (FileReader reader = new FileReader("test.txt")) {
            reader.read(buffer);
        }
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}

I don't see why it created a nested try-with-resources block instead of adding the resources to the existing try block, like this:
public static void main(final String[] args) {
    final char[] buffer = new char[100];
    try (FileReader reader = new FileReader("test.txt")) {
        reader.read(buffer);
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}
Comment 1 Jan Lahoda 2011-05-04 09:31:29 UTC
Feel free to reassign back.
Comment 2 Svata Dedic 2015-04-03 08:48:47 UTC
Fixed in jet-main#8e32a189adc1
Comment 3 Quality Engineering 2015-04-12 04:25:14 UTC
Integrated into 'main-silver', will be available in build *201504120001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/8e32a189adc1
User: Svata Dedic <sdedic@netbeans.org>
Log: #197884: existing pure try block can be converted to try-with-resources