Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Exceptions (suppressed vs ignored)

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there! I have this question from the Kathy Sierra book OCP Java SE 8 (Chapter 3, question 9)


Which exceptions will the code throw?
The correct answer is: D. "RuntimeException c with no suppressed exception"
The explanations says: "While the exception caught by the catch block matches choice A (IOException with suppressed RuntimeException a), it is ignored by the catch block. The catch block just throws RuntimeException c without any suppressed exceptions.
I really donĀ“t understand why the exception thrown by the close method is not a suppressed exception?

Thank you for the help!
 
Marshal
Posts: 26290
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good question.

I tracked down this tutorial: Java Suppressed Exceptions and found that section 2.3 is pretty much exactly the same scenario as the code you posted. (Note that Closeable is a subinterface of AutoCloseable.) And it says:

We should note that when using AutoCloseable, it's the exception thrown in the close method that's suppressed.



Okay, sure, it's possible that you and I and the tutorial are all mistaken (and the other tutorials I found with very similar scenarios too) and the meaning of "suppressed exception" is more complicated than that. But it's still a good question.
 
Javier Gonzalez
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul! Thank you for your answer. When I put this snippet of code in the catch block and I run it in Eclipse is printing: java.lang.RuntimeException: a
So we could think is an errata of the book? Because Eclipse is printing that there is a suppressed exception in the code and the book is saying the opposite.



 
Paul Clapham
Marshal
Posts: 26290
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure looks like it to me. But I don't take these exams and often there's some tricky things in the multiple choice answers. What were the other possible answers?
 
Enthuware Software Support
Posts: 4501
44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two things going on here -
1. The question asks, "Which exceptions will the code throw?" -
I am guessing the author(s) intends to ask which exception will the code throw out of the main method i.e. visible on the console.

2. An Autocloseable's close is called immediately after the end of the try block (and before the catch block).
[Ref:
Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.
This allows a catch clause of an extended try -with-resources statement to catch an exception due to the automatic initialization or closing of any resource. (JLS Section 14.20)]

So, when an exception is thrown in the try block as well as the close method, the exception received while closing the last of the resources will be thrown (with all other previously thrown exceptions added to the list of suppressed exception) and if there is an appropriate catch clause, it will be caught by that catch clause. Therefore, in the given code, the exception received in the catch clause inside the run method indeed gets "a" (i.e. the exception thrown by close with IOException as suppressed exception.

But that is not the exception being thrown from the catch block. That exception is not being used for anything. Instead, the given code creates a new exception with "C" and throws this new RuntimeException to the caller i.e. to main, which is what is thrown out to the console.

Therefore, the book's answer is correct.


 
Paul Clapham
Marshal
Posts: 26290
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:I am guessing...



This is why we need to see the actual question and the actual answer choices.
 
Marshal
Posts: 71682
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:. . . [Ref:  . . .

Did you forget the reference? Is this JLS section what you meant? It shows when the resource is closed, and also where the suppressed exception comes from.
 
Javier Gonzalez
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys! Sorry for the delay. The text says:

Which exceptions will the code throw?
A. IOException with suppressed RuntimeException a
B. IOException with suppressed RuntimeException c
C. RuntimeException a with no suppressed exception
D. RuntimeException c with no suppressed exception
E. RuntimeException a with supressed RuntimeException c
F. RuntimeException c with suppressed RuntimeException a
G. Compilation fails

D is correct. While the exception caught by the catch block matches choice A, it is ignored by the catch block. The catch block just throws RuntimeException c without any suppressed exceptions.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic