Here is the code: class X { public static void main(String[] args) { try { int i = (-1 >> 65535) + 1; int j = 5/i; System.out.println("Something"); } catch (Exception e) { System.exit(0); } finally { System.out.println("Finally");} } } When I have run this code nothing is displayed. I have thought that finally method is called no mather do we have or haven't Exception. Can someone clarify this. Thanks
Hi Abraham, finally block is always executed...except for some situations like this....i.e. System.exit(0); For this situation, the control directly comes out of the program w/o executing anything further...not even finally. HTH Rashmi ------------------------------------------- SCJP2
Originally posted by Abraham Lee: Here is the code: class X { public static void main(String[] args) { try { int i = (-1 >> 65535) + 1; int j = 5/i; System.out.println("Something"); } catch (Exception e) { System.exit(0); } finally { System.out.println("Finally");} } } When I have run this code nothing is displayed. I have thought that finally method is called no mather do we have or haven't Exception. Can someone clarify this. Thanks
Hi This is the only way you could get away without executing the finally clause, now instead of System.exit, if you had return then guess what the finally would execute. Hope this Helps!!! Amish
Originally posted by Abraham Lee: Here is the code: class X { public static void main(String[] args) { try { int i = (-1 >> 65535) + 1; int j = 5/i; System.out.println("Something"); } catch (Exception e) { System.exit(0); } finally { System.out.println("Finally");} } } When I have run this code nothing is displayed. I have thought that finally method is called no mather do we have or haven't Exception. Can someone clarify this. Thanks
Now see if you comment the System.exit(0),
You will get catch statement as well as finally part is also executed.
what is this System.exit ? i have seen this in someplaces... is this important for the exam ? just let me know the basics of System.exit , why and where we use it.
Originally posted by Amish A Patel:
Hi This is the only way you could get away without executing the finally clause, now instead of System.exit, if you had return then guess what the finally would execute. Hope this Helps!!! Amish
Abraham. Just remember one thing, incase if there is any System.exit() method in your source code, then there is no chance that your finally will execute. HTH Sonir
Guys. I just want to clear my doubt on this matter. "Does the 'return' word in the finally statement will ever affect the statement" If yes, then how? Sonir
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Gift giving made easy with the permaculture playing cards