• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Why is 5 getting printed here?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
u r exactly rght girish!!!
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!