• 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

Why is 5 getting printed here?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output: 1 4 5

Could anyone please explain why 5 is getting printed here? I thought when an exception occurs, control is transferred to the respective catch clause and then finally gets executed. After this, the program exits. Please correct me if I am wrong. Thanks!
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)When Exception is caught ctrl goes to finally block(if their is any),
and normal execution continues after finally block(as if Exception as not occured).
2)Think! When Exception is caught and handled, What is the necessary to exit.

3)If Exception is not caught then finally block is executed and remaining code after finally block is skipped, -->ctrl goes to calling method.
4)If you do not catch & handle Exception even there -->ctrl goes to its calling method...continues till you catch Exception.
[ May 12, 2006: Message edited by: Girish Nagaraj ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u r exactly rght girish!!!
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when an Exception is caught, the respective exception is executed and the finally block is excecuted and the programme continues from there...untill it reaches the stack....that is why we use Exception mechanism.

plz correct me if i am wrong

regards
krishna.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of try/catch is to handle the error and continue the programme without errors. So u handled the error and programme continues. Simple...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic