Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 6 - Review Question 20 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Raluca Dragoi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the output of the following contain? (Choose all that apply)

A. abce
B. abde
C. An exception with the message set to "1"
D. An exception with the message set to "2"
E. An exception with the message set to "3"
F. Nothing; the code does not compile.

The answer (in the book) is:
A, E. The code begins normally and prints a on line 13, followed by b on line 15. On
line 16, it throws an exception that’s caught on line 17. Remember, only the most spe-
cific matching catch is run. Line 18 prints c, and then line 19 throws another excep-
tion. Regardless, the finally block runs, printing e. Since the finally block also
throws an exception, that’s the one printed.

Now, I understand why the code prints: "a", "b", "c", but I don't understand why is "d" not printed.
In line 19, a RuntimeException is thrown but it is caught on line 20. So in this case, shouldn't "d" be printed?
I understand that the exception thrown on line 22 is not treated anymore so finally block runs, printing "e".

Thank you
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dragoi,
Welcome to CodeRanch!

Only one catch block in a "set" is chosen. This means that the catch block on line 20 does not catch the exception thrown on line 19.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dragoi Raluca,

First of all, a warm welcome to CodeRanch!

Dragoi Raluca wrote:Now, I understand why the code prints: "a", "b", "c", but I don't understand why is "d" not printed.
In line 19, a RuntimeException is thrown but it is caught on line 20. So in this case, shouldn't "d" be printed?
I understand that the exception thrown on line 22 is not treated anymore so finally block runs, printing "e".

In this topic you'll find an explanation about exactly the same question (with additional code examples and explanations as well). And in this topic and this one you'll find excellent explanations about which strange behavior you might encounter when the finally-block doesn't complete normally (as in this question). All threads are definitely worth reading!

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I've gone ahead and added the code tags for you. See how much easier the code is to read? If you want to see a few usage examples (and which options you can use), have a look at the Using Code Tags section of the Ranch Guide.
 
Raluca Dragoi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne and Roel for the helpful explanation and tips .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic