Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Possible errata chapter 6 (Java OCA 8 Programmer I Study Guide, Sybex)  RSS feed

 
Juliet Smith
Greenhorn
Posts: 9
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the code example for reading a file is confusing, since the if statement in the try block will never be true. If an exception is thrown then reader will always be null, as the read method will not return a value if an exception is thrown.



In fact IntelliJ IDEA shows a warning about the condition always being false
 
Juliet Smith
Greenhorn
Posts: 9
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget the page number its p. 312
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juliet Smith wrote:I think the code example for reading a file is confusing, since the if statement in the try block will never be true. If an exception is thrown then reader will always be null, as the read method will not return a value if an exception is thrown.

You are absolutely correct! But I think the authors wanted to save a few lines and create a concise code snippet. In actual (production) code you would never have a read() method which returns a FileReader. This would be a violation of the single responsibility principle. You would have a method responsible for creating the FileReader instance and another one to process its contents. The new and improved code snippet would look like this (and now the if statement is definitely required)

Hope it helps!
Kind regards,
Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36634
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. The point of that example is to show try/catch. So not an errata; it was intentionally written that way .

Also, the real exam shows "concise" code snippets sometimes so we try to get you used to it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!