• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Sybex CSG 17: Appendix: Answers To The Review Questions, Chapter 13 Concurrency, Question 4

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also choose E. Line w4 as one of the correct alternatives because get() may throw two checked exceptions that we need to handle, InterruptedException and ExecutionException.
 
author & internet detective
Posts: 41146
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mohamad,
Welcome to CodeRanch!,
While the method is allowed to throw exceptions under certain circumstances, I don't see anything that suggests they would in this example.
 
Mohamad Almasri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for welcoming me, Jeanne. I am glad to be part of this group.
I agree with what you said. The code might not throw any exception at all but if we go back to question 4, chapter 13 the question says:
Which lines need to be changed to make the code compile?
If I am correct the code will not compile because it fails to honor the Catch or Specify Requirement for checked exceptions. Line w4 must be enclosed by a try statement that catches the exceptions or we must provide a throws clause in order for it to compile.

I have attached a screenshot of the compilation error when I try to compile the code
get.PNG
[Thumbnail for get.PNG]
 
Jeanne Boyarsky
author & internet detective
Posts: 41146
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree it can throw an exception. However, the code is not in a main method in the book. When not in a method, you have to assume the context is correct. In this case, that it handles the exception.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic