• 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

OPC Chapter 9, Question 3: B is not correct answer despite code isn't catching IOException

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Question No. 3, for Chapter 9, the correct answer is presented as D only, though I agree with that I believe "B" is correct too since Files.readAttributes() throws checked IOException and the code snipped isn't catching it, therefore code won't compile on line 2 too.

Please correct if I'm missing the point, thanks!
Files.readAttributes.png
[Thumbnail for Files.readAttributes.png]
 
author & internet detective
Posts: 41860
908
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
Pretend the line numbers aren't there. After we "printed", Oracle announced that code snippets can be treated as ignoring thrown exceptions.

Plus the line numbers are wrong. There's no class definition so that can't be line 1!
 
Artem Lovan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense, thanks!

But now I have a follow up question on "Oracle announced that code snippets can be treated as ignoring thrown exceptions." Does it mean in code snippets where exception needs to be catches, we should be aware that there might now be any code that catches an exception but we shouldn't see it as incorrect code because Oracle announced it as "fine" for a code snippet? I would really appreciate an explanation since that of cause can play big role of what folks might select as correct answer on the exam.

Thanks!
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
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
The relevant rule is:

Code fragments: A code fragment is a small section of source code that is presented without its context. Assume that all necessary supporting code exists is present and that the supporting environment fully supports the correct compilation and execution of the code shown and its omitted environment.



Basically, you have to pay attention to exceptions if the code is in a method.

And the full list of things you can now assume is

Missing package and import statements: If sample code do not include package or import statements, and the question does not explicitly refer to these missing statements, then assume that all sample code is in the same package, and import statements exist to support them.
No file or directory path names for classes: If a question does not state the file names or directory locations of classes, then assume one of the following, whichever will enable the code to compile and run:
All classes are in one file
Each class is contained in a separate file, and all files are in one directory
Unintended line breaks: Sample code might have unintended line breaks. If you see a line of code that looks like it has wrapped, and this creates a situation where the wrapping is significant (for example, a quoted String literal has wrapped), assume that the wrapping is an extension of the same line, and the line does not contain a hard carriage return that would cause a compilation failure.
Code fragments: A code fragment is a small section of source code that is presented without its context. Assume that all necessary supporting code exists is present and that the supporting environment fully supports the correct compilation and execution of the code shown and its omitted environment.
Descriptive comments: Take descriptive comments, such as "setter and getters go here," at face value. Assume that correct code exists, compiles, and runs successfully to create the described effect.

 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic