Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

try/catch/final - oca question (sybex)

 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this question by email. Posting it here and will answer in one minute.

email wrote:On page 307, it says that if an exception is thrown, then the finally block runs after the catch block. If no exception, then the finally block runs after the try block.

However, on page 312, there's a code snippet that looks like this:

Then on page 313, it says the finally block runs after the try block. The thing is, page 307 told me that the catch block has to be executed because the try block throws a Runtime Exception. I'm really confused about this contradiction. So why does the finally block run after the catch block? Is the catch block just ignored entirely?

 
Jeanne Boyarsky
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "trick" to understanding this example is to add printlns



This prints:
in try
in catch
in final
(and then throws Exception)

Which shows you that the try block is run first as always. The catch block comes next since the try block throws an exception. Then the finally block runs last as always. Since it throws an exception, you get that one instead of the one from the catch block. But all three run!
 
Marshal
Posts: 74067
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your correspondent has misunderstood what you said. Yes, control goes from line 2→3→4 (first code block) to the catch. That causes an unchecked exception to be thrown, but it is not the same object as in line 2. Then the finally is executed later; the plain simple Exception from line 6 supersedes the unchecked exception in line 4 (I am sure supersedes isn't quite the right word).
Maybe you could regard it as,
  • 1: The execution starts in the try.
  • 2: The catch is only executed if an appropriate exception is thrown in the try.
  • 3: The finally is always executed, and always last.
  •  
    Campbell Ritchie
    Marshal
    Posts: 74067
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If the finally follows a catch, it is also after the try. It doesn't say anything about directly after anywhere, does it?
    reply
      Bookmark Topic Watch Topic
    • New Topic