Before you say that this question has already been answered tons of times, here is my code snippet:
final int x;
try {
x = blah();
} catch (MyPanicException e) {
abandonEverythingAndDie();
}
System.out.println("x is " + x);
If invoking abandonEverythingAndDie() has the effect of ending the execution of the whole program (say because it invokes System.exit(int) ), then x is always initialized whenever it is used.
Is there a way in the current Java language to make the compiler happy about variable initialization, by informing it that abandonEverythingAndDie() is a method which never returns control to the caller?
I do not want to
- remove the
finalkeyword - initialize
xwhile declaration, - nor to put the
printlnin the scope of thetry...catchblock.
xisfinal, that's not possible. - Florent Baylefinalvariable twice? Once at point of declaration then insidetry? - Kuba