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 243830 - "The assigned value is never used" hint versus "variable x might not have been initialized" compiler error after System.exit() in catch block
Summary: "The assigned value is never used" hint versus "variable x might not have bee...
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: PC Linux
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
: 262557 (view as bug list)
Depends on: 237893
Blocks: 249320
  Show dependency tree
 
Reported: 2014-04-14 18:35 UTC by sparkee
Modified: 2016-07-26 21:14 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 sparkee 2014-04-14 18:35:20 UTC
A "The assigned value is never used hint" is in code that follows a catch block with an embedded System.exit(). This is actually technically correct
because if the exception is thrown the rest of the code cannot be reached.  However, removing the initializer for the variable causes a 
"variable x might not have been initialized" compiler error to appear (which is technically incorrect).

Which leaves us faced with a choice between a hint and an error visible in our sources...

This example demonstrates:
================================================

class Demonstration {

  public static void main(String[] args) {

    int n1 = 10;  // No hint here (as is correct)
    try {
      n1 = Integer.parseInt("32");
    } catch (NumberFormatException ex1) {
      System.out.println("Whoops, bad integer string!");
    }
    int m1 = n1;

    int n2 = 10;  // Hint here (the assigned value is never used - technically correct!)
    try {
      n2 = Integer.parseInt("32");
    } catch (NumberFormatException ex2) {
      System.exit(1);
    }
    int m2 = n2;

  }

   int n3; 
    try {
      n3 = Integer.parseInt("32");
    } catch (NumberFormatException ex3) {
      System.exit(1);
    }
    int m3 = n3;  // Compiler error here (variable n3 might not have been initialized - technically incorrect!)

  }
}
================================================
Comment 1 Jiri Prox 2014-04-14 21:17:38 UTC
The error is provided directly by the java compiler. (I suppose compiler does not check for System.exit statement). Netbeans have to only display all compilation error to user.
Comment 2 Jan Lahoda 2014-04-15 09:12:16 UTC
The error "variable <x> might not have been initialized" is indeed correct (the Java Language Specification specifies that a local variable or blank final field must be definitely assigned before it is accessed, and also defines when the variable is definitely assigned - see JLS chapter 16).

What is not correct, I think, is the "the assigned value is never used" warning - I don't think that should be shown in cases where removing the assignment would lead to compile-time errors.
Comment 3 sparkee 2014-04-15 09:59:44 UTC
I was a bit reluctant to file this as a bug.  The difference clearly is that the compiler does not check for system.exit().  I really think that it is a compiler bug, but I don't imagine we would have much luck in getting that fixed.

There is a workaround, and perhaps technically a more proper approach anyway.  Instead of using system.exit(), throw an Error out of the catch block and then in an outer catch block for the error do a System.exit().
Comment 4 sparkee 2014-04-15 13:31:21 UTC
One additional comment.  See

http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6914112

Given this, I think the proper answer is for the hint system to NOT consider System.exit() or runtime.exit() as terminating conditions (despite the fact that the section 12.8 Java Language Specification clearly defines these two cases as terminating conditions for the JVM).
Comment 5 Svata Dedic 2015-09-10 14:17:38 UTC
The proper fix requires very peculiar enhancements in the data flow: while unused assignment should not be reported for assignments, which go off the scope because of System.exit(), the flow must inform that the value is effectively dead - other refactoring rely on the flow algorithm.
Comment 6 Svata Dedic 2015-11-11 08:24:24 UTC
Fixed in experimental impl, wait for the umbrella to close.
Comment 7 Svata Dedic 2016-07-26 21:14:14 UTC
*** Bug 262557 has been marked as a duplicate of this bug. ***