aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finally method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finally method" Watch "Finally method" New topic
Author

Finally method

Abraham Lee
Greenhorn

Joined: Jan 14, 2002
Posts: 4
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
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
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
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
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
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
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finally method
 
Similar Threads
throw/catch exception
Enthuware questions
Probelm with equals method
How do you answer such questions?
When the Statment After Finally clause will get Executed