Win a copy of Kotlin for Android App Development this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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

 
author & internet detective
Posts: 39129
723
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: 39129
723
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: 62846
203
  • 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: 62846
    203
    • 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?
     
    Don't listen to Steve. Just read this tiny ad:
    Programmatically Create PDF Using Free Spire.PDF with Java
    https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!