This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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.