The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is 5 getting printed here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is 5 getting printed here?" Watch "Why is 5 getting printed here?" New topic

Why is 5 getting printed here?

Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51

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!
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
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 ]
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
u r exactly rght girish!!!
Krishna Bulusu
Ranch Hand

Joined: Jan 27, 2006
Posts: 32
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

Sreeraj Harilal
Ranch Hand

Joined: May 09, 2006
Posts: 45
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...

SCJP 5.0<br />SCWCD 1.4<br />SCBCD 1.3<br />SCEA part 1
I agree. Here's the link:
subject: Why is 5 getting printed here?
It's not a secret anymore!