when i ran this code it gives me null pointer exception followed by 0. i know that i points to null and when we did System.out.println(i.length) it gives runtime exception . but according to me since the nested try block does not have any associated catch block , so when an exception is throw in it , the control will go inside finally block , which assigns reference of eye to i . but i know that catch block runs which prints the output . i want to know is that how come catch block gets executed since inner try block does not have any associated catch block with it. please explain
The presence of the finally block doesn't stop the exception from propagating out to the enclosing try/catch. It just means that before that happens, the code inside the finally block will execute. If the inner try block were accompanied by a catch, then the exception wouldn't propagate outwards.