• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in flow control in the folowing code !!!

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks srinibash
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic