File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Query on RHE mock exam answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Query on RHE mock exam answer" Watch "Query on RHE mock exam answer" New topic
Author

Query on RHE mock exam answer

LadyMahler
Ranch Hand

Joined: Jan 08, 2002
Posts: 63
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
LadyMahler
Ranch Hand

Joined: Jan 08, 2002
Posts: 63
Wow, that was a speedy reply...
Thanks a lot! It makes sense now.
Adi
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
At Javaranch you get your answer right away or else you get your money back
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Hmm, why not triple your money back or your second pizza is free??
Rob


Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query on RHE mock exam answer