File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finally method

 
Abraham Lee
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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
 
Ruchi Kolla
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
mark stone
Ranch Hand
Posts: 417
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should look at the System class in the JavaDocs.
System.exit() causes the VM to shutdown/quit.
Rob
 
sonir shah
Ranch Hand
Posts: 435
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic