I have a question for which I myself have a very intuitive answer. This question relates to the try-catch-finally blocks in Java. Well just the other day I was trying out something and I bumped into this compilation error. I have also gone through Do you really need the 'finally' block which answers lot of my doubts(especially comment #2).
What I am looking for here is why do I see a compilation error when I comment lines from #11 to #13(basically I am commenting the inner catch block). And the compilation and run-time execution runs fine when uncomment the same lines. If I could get an answer more from a OOPS context, it would increase my knowledge & will be of great help.
Thanks in advance. !!
public class TryCatchFinally {
public static void main(String[] args) throws Exception {
try {
System.out.println("Try...");
throw new Exception();
} catch (Exception e) {
System.out.println("Catch...");
try {
System.out.println("Inner Try");
throw new Exception();
} /*catch(Exception e1) {//Line #11
System.out.println("Inner catch");
}*///Line #13
finally{
System.out.println("Inner finally");
}
System.out.println("Going out of catch..");//Line #17
}
finally{
System.out.println("Finally");
}
System.out.println("Going out of try..");//Line #22
}}
As others have observed, you throw an Exception
on line 10. What happens after that?
When the inner catch
block on lines 11-13 is uncommented, you'll throw the Exception
on line 10 and immediately catch it. You'll print out "Inner catch", because that's what's in the catch
block, then "Inner finally". The exception is no longer active since you caught it, so you proceed to line 17 and beyond.
When the inner catch
block is removed, you'll throw the Exception
on line 10 and go straight to the finally
block on line 14. But the exception hasn't been caught, so it's still active. When you exit the inner finally
, there is no other catch
block, so your method will return immediately and pass the exception up the call chain. (That's not very far in this case, since it's the main
method.) The point is that the println
on line 17 can never execute because of the exception.
The Java compiler is smart enough to figure out that there is no possible execution path through your program where line 17 gets executed, so so it gives you a compilation error for it (and for line 22, too).
You don't need the inner catch if you don't need to do anything after your finally
blocks. If you want to keep the method alive and execute something after your finally
blocks, you need to keep the inner catch
.
The last line of your inner try throws an exception, meaning only the finally blocks will be executed.
add another catch block, or remove that exception throw, and your problem is solved.
public class TryCatchFinally {
public static void main(String[] args) throws Exception {
try {
System.out.println("Try...");
throw new Exception();
} catch (Exception e) {
System.out.println("Catch...");
try {
System.out.println("Inner Try");
throw new Exception();
} catch(Exception e1) {//Line #11
System.out.println("Inner catch");
}//Line #13
finally{
System.out.println("Inner finally");
}
System.out.println("Going out of catch..");//Line #17
}
finally{
System.out.println("Finally");
}
System.out.println("Going out of try..");//Line #22
}}
or this
public class TryCatchFinally {
public static void main(String[] args) throws Exception {
try {
System.out.println("Try...");
throw new Exception();
} catch (Exception e) {
System.out.println("Catch...");
try {
System.out.println("Inner Try");
// throw new Exception();
} /*catch(Exception e1) {//Line #11
System.out.println("Inner catch");
}*///Line #13
finally{
System.out.println("Inner finally");
}
System.out.println("Going out of catch..");//Line #17
}
finally{
System.out.println("Finally");
}
System.out.println("Going out of try..");//Line #22
}}
will work.
At 10th line , You are throwing an exception which needs to be handled. Since you are not handling that exception , jvm is unable to compile the code after 10th line . That,s why jvm is giving the error "unreachable code" . So the solution is to either handle that exception by catching that exception or dont throw exception at 10th line.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.