Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

suppressing of exception

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book, chapter 7, example 10

which exception will code throw:
1. RuntimeException c with suppressed RuntimeException a
2.RuntimeException c with no suppressed exception

according to me answer should be 1, but unfortunately I am not correct, correct answer is 2
can someone explain why??
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Puspender,

You haven't invoked the method close() in your code, therefore option 1 cant be correct.



Regards,

Kondwani.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, Kondwani
it is try-with-resource, so whenever this blocks ends up, the close() is automatically called for closing the files declared in try() .
so here also close() will be called for the object t
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
someone please help me out !!!
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

which exception will code throw i think this sentence mean that exception that are handled cant be result that code throw. I think that this sentence mean resulted not handled exception of code
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergej Smoljanov wrote:which exception will code throw i think this sentence mean that exception that are handled cant be result that code throw. I think that this sentence mean resulted not handled exception of code


sorry sergej, but couldn't get yours words !!
and your code shows that the exception thrown by close() gets suppressed, but why the book questions is answer different ?
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which exception will code throw.
IOException is handled, RuntimeException("From close()") is supressed. and result of code is: RuntimeException("code throw this as result of execution") and it has no suppressed baggage.


i mean that from your code lines from 1 to 12 has result of RuntimeExceptin c with no suppressed exception. Some hint given in answer:
1. RuntimeException c with suppressed RuntimeException a
2. RuntimeException c with no suppressed exception
that hint that question about this exception
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you sergej for previous code which explains much about my problem , but does it mean that whenevr any new exception is encountered(in your previous code//line16) ,the suppressed exceptions are removed ??
what is the life of those suppressed exception, I mean till when they can be called by getSuppressed() ???
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it not removed because this other exception that not related to exception that was throwed inside try block - this is other not related object.

Note i use saved variable to show that exception just another object. And suppressed exception is part of state of this object.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so it's all about from which reference variable are we calling the getSuppressed() ??
And I think reason for my confusion is that book's question has not mentioned the getSuppressed() .
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic