• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on RHE mock exam answer

 
LadyMahler
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following question is asked (Chapter 5, Question 9 in second edition of RHE):
"Which one of the following fragments shows the most appropriate way to throw an exception? Assume that any undeclared variables have been appropriately declared elsewhere and are in scope and have meaningful values."
The correct answer is given as:
B) 1. if (!f.exists()) { // f is a File object
2. throw new IOException("File " + f.getName() + " not found");
3. }
Yet, if the file is not found, it won't be able to get to its hands on the method "getName". Is my reasoning right here? The correct answer should then be:
E) 1. if (!f.exists()) { // f is a File object
2. throw new IOException();
3. }
Please let me know what I'm missing...
[ January 15, 2002: Message edited by: Adi du Toit ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adi,
The method exists() tests for the presence of the file on the actual filesystem. You may create a new File object (and thus give it a name) but the File is not yet created on the filesystem until you write to or read from it (by means of output/input stream). Thus the method getName will return the name used in the creation of the File object. Remember that the File object has no physical presence on the filesystem.
Answer E is not accurate enough, since you don;t really know what causes the IOException to be thrown (you don't have any message, bad practice)
HIH
[ January 15, 2002: Message edited by: Valentin Crettaz ]
 
LadyMahler
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that was a speedy reply...
Thanks a lot! It makes sense now.
Adi
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At Javaranch you get your answer right away or else you get your money back
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, why not triple your money back or your second pizza is free??
Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic