• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

try/catch/final - oca question (sybex)  RSS feed

 
author & internet detective
Posts: 39234
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 39234
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 64100
215
  • Mark post as helpful
  • send pies
  • 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: 64100
    215
    • Mark post as helpful
    • send pies
    • 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?
     
    A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
    Create Edit Print & Convert PDF Using Free API with Java
    https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!