• 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

Query on RHE mock exam answer

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, that was a speedy reply...
Thanks a lot! It makes sense now.
Adi
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At Javaranch you get your answer right away or else you get your money back
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, why not triple your money back or your second pizza is free??
Rob
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic