Why doesn't the compiler understand that the variable is initialized in either the try
or the catch
block
and complains at the finally
block?
int i;
try {
i = 0;
}
catch (Exception e) {
i = 2;
}
finally {
System.out(i);
}
If the initialization statement (i = 0;
) fails, then the program will continue with the finally
block, where the variable will still be un-initialized and this is why you get a compile-time error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With