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 215629 - Applying assist surround with try-catch for multiple variable assignments results into compile errors
Summary: Applying assist surround with try-catch for multiple variable assignments res...
Status: REOPENED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 7.1.2
Hardware: PC Windows 7
: P4 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on: 238925 238921
Blocks:
  Show dependency tree
 
Reported: 2012-07-17 13:25 UTC by Jachym_Vojtek
Modified: 2016-07-11 06:55 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 Jachym_Vojtek 2012-07-17 13:25:56 UTC
Consider following class:

import java.io.IOException;
import java.text.ParseException;

public class SurroundWithTryCatchTest {

    int throwSomething() throws IOException {
        return 1;
    }
    
    int throwSomething2() throws Exception, ParseException {
        return 1;
    }

    void foo() {
        // Some comment1
        int i1 = throwSomething(), i2 = throwSomething2();
        // Some comment1

        System.out.println(i1);
        System.out.println(i2);
    }
}

Apply light bulbs on line:
int i1 = throwSomething(), i2 = throwSomething2();
 
1) Applied "Surround Block with try-catch" (the first one offered) does not catch Exception 
2) Applied "Surround Block with try-catch" (the second one offered) catches Exception before ParseException
3) Applied "Surround Statement with try-catch" (the first one offered) results into uncompilable code (for example duplicate declaration of i1)
4) Applied "Surround Statement with try-catch" (the second one offered) results into uncompilable code
Comment 1 Svata Dedic 2013-11-28 10:35:56 UTC
Well, each of the "surround statement", "surround block" will solve part of the issue - for the method call it was generated for. 
The result of each of the actions is (except for duplicating the comments) OK from thar perspective. Note that fixing just one of the two problems lead to uncompilable code. 

I did not reproduce (3), each variable was declared just once in 8.0. It is really broken in <= 7.4.

To the reported cases:

1/ correct result; throwSomething() invocation was corrected, which throws just IOException.

2/ the same result as if throwSomething2() was used in other context than variable decl group. In general declaring that a method throws both a subclass and superclass is valid, though discouraged. If you don't agree with the handling, file a separate issue.

3/ not reproduced (see above), otherwise correct.

4/ correct result.

Things observed, which DO count as defects:
* Surround statement (1st) will duplicate // comments

* fixes for throwSomething2() are offered although the caret is positioned at throwSomething() invocation and vice versa. The compiler reports more precise offsets for the produced error messages, only the relevant parts of the code should be highlighted and each of the highlights should only offer 'its own' fixes - or at least prioritize its own fixes to the top of the list.
Comment 2 Svata Dedic 2014-04-11 09:21:08 UTC
IMHO the result is +- fine - both partial catches are generated correctly, the variable group is split OK.

Still the trailing comment is duplicated if 2nd "surround statement" is selected.

Degrading to P4.
Comment 3 Jachym_Vojtek 2014-04-11 10:12:46 UTC
Let us stay for now only with case 1)

If I apply "Surround Block with try-catch" to block
{
  // Some comment1
  int i1 = throwSomething(), i2 = throwSomething2();
  // Some comment1

  System.out.println(i1);
  System.out.println(i2);
}
I do expect that all the exceptions from the "Surrounded Block" will be treated somehow.

As a result the block is surrounded by try/catch, but only first exception is caught.

try {
  int i1 = throwSomething(), i2 = throwSomething2();
  // Some comment1
  System.out.println(i1);
  System.out.println(i2);
} catch (IOException ex) {
  Logger.getLogger(SurroundWithTryCatchTest.class.getName()).log(Level.SEVERE, null, ex);
}

No, this is not properly applied "surround block" with try/catch.
Comment 4 Martin Balin 2016-07-07 07:18:05 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss
Comment 5 Jachym_Vojtek 2016-07-11 06:55:28 UTC
I checked this using  NetBeans IDE Dev (Build 201607110002).
At least "Surround statement with try-catch" leads to compile error "variable i1 might not have been initialized".
See the result:

    void foo() {
        // Some comment1
        int i1;
        try {
            i1 = throwSomething();
        } catch (IOException ex) {
            Logger.getLogger(SurroundWithTryCatchTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        int i2 = throwSomething2();
        // Some comment1

        System.out.println(i1);
        System.out.println(i2);
    }