• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic