• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

exception handling

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Example {
static int c = 3;
public static void main(String [] args) {
System.out.print("Kristin wants ");
try {
System.out.print("a bike, ");
if(c > 0) {
throw new Exception();
}
System.out.print("a robot, ");
}
catch (Exception e) {
System.out.print("a dollhouse, ");
System.exit(0);
}
finally {
System.out.print("and an American Girl doll.");
}
}
}

In the above code ...finally block is not executed. Can anyone explain why??
o/p is �Kristin wants a bike, a dollhouse,� then the program exits.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your program will stop at System.exit(0)
 
Ranch Hand
Posts: 982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i believe that System.exit(0) is the only way where we can stop the execution of a final block from happenning....

Correct me if am wrong...


Regards
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Kumar, you wrote the correct answer. System.exit(0) is used for terminating the program.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to the above point, the only way to escape from executing the finally block is using the System.exit(0) statement . Otherwise, the finally block is sure to execute...
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following are 4 incidences where finally can be prevented

1. The death of the thread.

2. The use of System.exit()

3. Interruption of power supply to the CPU.

4. An exception arising in the finally block itself
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks rajesh.
that was great.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajesh...Could u please refer us a good doc on the same.
 
reply
    Bookmark Topic Watch Topic
  • New Topic