• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B OCP7 - Chap 7- Page 412

 
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Need help with the following question, can someone elaborate on the answer provided in the book.
The question is as follows:
10. Given:

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 suppressed RuntimeException c
F. RuntimeException c with suppressed RuntimeException a
G. Compilation fails

According to me the answer should have been F, but the answer is D, I am not clear with the explanation to the answer. I tried modifying the code to the following to verify if there is any suppressed exception, and I could see that there is indeed one.


output:
Suppressedjava.lang.RuntimeException: a
Exception in thread "main" java.lang.RuntimeException: c
at com.explore.pallavi.Animals.run(Animals.java:25)
at com.explore.pallavi.Animals.main(Animals.java:15)
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pallavi the question is asking about the final exception that's thrown. Let's modify the code a bit:

The stack trace you'll see doesn't have any suppressed exception, thus the answer is correct. But if I modify the code a bit, you'll be able to see the suppressed exception in the output:

Now the final output will contain suppressed exception. Hope that helps...
 
Pallavi Sadit
Ranch Hand
Posts: 50
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit. I understood now, the IOException and Suppressed exception are caught by the catch block , and finally a RuntimeException is thrown which is uncaught. So that is the one that is thrown at the end of the code and hence the answer.
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic