How can I avoid the "local variable may not have been initialized" Java compilation error? (Yes seriously!)

Before you say that this question has already been answered many times, here is my code snippet:

final int x;
try {
    x = blah(); 
} catch (MyPanicException e) {
    abandonEverythingAndDie();
}
System.out.println("x is " + x);

If the call abandonEverythingAndDie()terminates the execution of the entire program (for example, because it calls System.exit(int)), then it is xalways initialized whenever it is used.

Is there a way in the current Java language that the compiler is happy with the variable initialization, saying that it abandonEverythingAndDie()is a method that never returns control to the caller?

I < don't want

  • delete keyword final
  • initialize xat the time of declaration
  • and do not place printlnin the area try...catch.
+4
5

, :

final int x;
try {
    x = blah();
} catch (MyPanicException e) {
    abandonEverythingAndDie();
    throw new AssertionError("impossible to reach this place"); // or return;
}
System.out.println("x is " + x);

abandonEverythingAndDie() - ( , , , ) return abandonEverythingAndDie():

final int x;
try {
    x = blah();
} catch (MyPanicException e) {
    return abandonEverythingAndDie();
}
System.out.println("x is " + x);

:

private static <T> T abandonEverythingAndDie() {
    System.exit(1);
    throw new AssertionError("impossible to reach this place");
}

throw abandonEverythingAndDie();

private static AssertionError abandonEverythingAndDie() {
    System.exit(1);
    throw new AssertionError("impossible to reach this place");
}
+5

, , . abandonEverythingAndDie , , . :

final int x;
try {
    x = blah(); 
} catch (MyPanicException e) {
    abandonEverythingAndDie();
    System.exit(-1); // the System.exit() itself is treated as a blackbox :)
}
System.out.println("x is " + x);

, "" , .

+4

, - ...

final int x;

int temp;   
try {
    temp = blah(); 
} catch (MyPanicException e) {
    abandonEverythingAndDie();
}
x = temp;
System.out.println("x is " + x);

Simple.

+2

, - x, blah() ? x , , Java . , , c, . println .

try {
    final int x = blah(); 
    System.out.println("x is " + x);
} catch (MyPanicException e) {
    abandonEverythingAndDie();
}

, SRP , , - , , , . , , , .

public void doBlah throws MyPanicException {
    final int x = blah(); 
    System.out.println("x is " + x);
}

public void tryBlahOrDie {
    try{
      doBlah();
    } catch (MyPanicException e) {
        abandonEverythingAndDie();
    }
}
+1

, ,

throw new RuntimeException()

return;

catch

final int x;
try {
    x = blah(); 
} catch (MyPanicException e) {
    abandonEverythingAndDie();
    throw new RuntimeException(); // here
}
System.out.println("x is " + x);

Even if this addition is thrownot executed due to abandonEverythingAndDie, the compiler will know that the control flow from this block catchcannot return to System.out.println("x is " + x);, so it will not require initialization x.

+1
source

All Articles