• 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 order of exception

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the K&B mock


In the above code the order of execution turns out to be
m1catch m2finally except.

My question is if m2() isnt providing a catch yet the main() IS how come when m2() is called, the finally of m2() is shown first and then the catch in main() is displayed? I though if the try block fails, flow control immidiately moves to the catch block and aftre that the finally and the program ends.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an exception is not caught, the finally is executed, but all other code is skipped.

Here, in your example, the exception IS being caught. If an exception is caught, code flow returns to normal.

m1() throws an exception, but it catches it. So code flow returns to normal. Then m2() is run. It throws another exception, which is not caught in m2(). The finally-block runs anyway, as the finally block ALWAYS runs. The code jumps ahead to the next catch-block in main() where it is caught. After this, code flow returns to normal. Program exits without an error.

If there would be a method, say m3(), called after the call to m2(), this call would be skipped.

You can have nested try-catch-blocks. When an exception is thrown, the remainder of the try-block is skipped. If the exception is not caught, the finally-block (if there is one) ALWAYS runs. Once this try-catch-block is finished, one might still be in a higher-level try-catch-block. In this case, the remainder of the higher-level try-block is skipped. There is another chance for catch or finally (or both). There can be many levels of try-catch.


[ July 29, 2006: Message edited by: Douglas Chorpita ]
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Doug, Thats a solid explanation, its clear now.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic