wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in flow control in the folowing code !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in flow control in the folowing code !!!" Watch "doubt in flow control in the folowing code !!!" New topic
Author

doubt in flow control in the folowing code !!!

Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
here is a question from K&B Quiz(which was in the CD given with the book) ::



Ques:: Determine the O/P
Ans:: m1catch m2finally except

I don't get why the method m2() was called once m1() threw exception..??
according to me o/p should be:: m1catch except
As soon as the contol returned from m1(), the control should have transferred to catch block in main()method. How come m2() was also called??
please explain !!!
[ July 16, 2007: Message edited by: Priyam Srivastava ]

"History would be kind to me, for I intend to write it."
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
m1 threw an exception but m1 also caught the exception. Therefore no exception was returned to main.

when you see a method that says throws Exception it means that method may or may not throw an Exception.


Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
I have a doubt here. In case m2() was provided with a catch, would then the except in main got printed?
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169
in case m2() also provided catch the o/p is::
m1catch m2catch. m2finally

but my doubt regarding the original code is still not clear..
srinibash udayasingh
Greenhorn

Joined: Jul 09, 2007
Posts: 22
Hi
since the exception thrown by m1() is catched .
no exception returns to main().but in case of m2() , the exception was not handled in m2().so it will returns to main().but as you know finally block must execute.so before control comes to main(),first finally block of m2() execute then comes to main method where this exception was handled.....so this out put.
you can understand the control flow easily if you add one catch(Exception e){Sytem.out.println("m2catch")} in m2(), hope you get the solution.

Thanks & Regards
Srinibash
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Thanks srinibash
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in flow control in the folowing code !!!