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
Rashmi Tambe
Ranch Hand
Joined: Aug 07, 2001
Posts: 418
posted
0
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
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Ruchi Kolla
Greenhorn
Joined: Jan 24, 2002
Posts: 19
posted
0
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.
mark stone
Ranch Hand
Joined: Dec 18, 2001
Posts: 417
posted
0
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
Rob Ross
Bartender
Joined: Jan 07, 2002
Posts: 2205
posted
0
You should look at the System class in the JavaDocs. System.exit() causes the VM to shutdown/quit. Rob
Rob
SCJP 1.4
sonir shah
Ranch Hand
Joined: Nov 01, 2001
Posts: 435
posted
0
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
sonir shah
Ranch Hand
Joined: Nov 01, 2001
Posts: 435
posted
0
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