• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finally method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look at the System class in the JavaDocs.
System.exit() causes the VM to shutdown/quit.
Rob
 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic