public class Test {
public static void main(String[] args) {
int a;
// for( a=0; a<5; a++ ){}
try {
a=10;
} catch (Exception e) {
//TODO: handle exception
}
System.out.println(a);
}
}
In the above code, initialization to var a by for loop persists while printing it, but not with try block.
intcannot have aNullPointerExceptionas it cannot be null. As far as the compile is concerned, there is a code-path (the catch block) that doesn't initializea, so it simply disallows it.