• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in flow control in the folowing code !!!

 
Priyam Srivastava
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Louis Moloney
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks srinibash
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic