aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does File.delete() throw an IOException? 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 "Does File.delete() throw an IOException?" Watch "Does File.delete() throw an IOException?" New topic
Author

Does File.delete() throw an IOException?

Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

Hi,
Directly from this API http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete%28%29:


delete

public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.


(emphasis mine)

here is the source-code taken from here:



It does not seem to throw an IOException.
Am I missing something?

Thanks in advance.


OCAJP 7, OCPJP 7
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

The JavaDoc for open jdk delete (from your link) is:
Throws:
java.lang.SecurityException If a security manager exists and its java.lang.SecurityManager.checkDelete(java.lang.String) method denies delete access to the file
Since:
1.2
See also:
delete()


Implementations of a method can be less restrictive in what exceptions they throw. I expect Oracle's implementation of delete() does in fact throw an IOException if the file cannot be deleted.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
    
    7
JavaDoc API description is the contract not the source code because, theoretically, source code for any third party library is not available. So if the documentation says it throws then it throws
At the same time if the implementation doesn't have the throws clause, I would say it is a bug and now they can't even fix it in a future version because that will break backward compatibility.

HTH,
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

Jeanne Boyarsky wrote:The JavaDoc for open jdk delete (from your link) is:
Throws:
java.lang.SecurityException If a security manager exists and its java.lang.SecurityManager.checkDelete(java.lang.String) method denies delete access to the file
Since:
1.2
See also:
delete()


Implementations of a method can be less restrictive in what exceptions they throw. I expect Oracle's implementation of delete() does in fact throw an IOException if the file cannot be deleted.


I have actually tried to use delete with a non-empty directory twice, first time the directory got deleted, second time instead of throwing an IOException it does not do anything.
And as Paul has noted the method signature does not even declare "throws IOException" or any other exception. Therefore the API are wrong.
If anybody knows a way to make the "hidden" IOException trigger, please do post it in this thread.

Thanks in advance.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Nick Widelec wrote:If anybody knows a way to make the "hidden" IOException trigger, please do post it in this thread.

You can't with that JDK. As Paul noted (more eloquently than me), the source code simply doesn't throw it. That doesn't mean that other JDKs are properly implemented and do throw the exception though.
Nick Widelec
Ranch Hand

Joined: Feb 28, 2013
Posts: 226

Jeanne Boyarsky wrote:
Nick Widelec wrote:If anybody knows a way to make the "hidden" IOException trigger, please do post it in this thread.

You can't with that JDK. As Paul noted (more eloquently than me), the source code simply doesn't throw it. That doesn't mean that other JDKs are properly implemented and do throw the exception though.


I am using the oracle JDK, I posted that jdk source code as that's what I found online.

This is my java version:

java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b11)

I have never installed other jdk than Oracle's.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Perhaps you just haven't done enough tests. Perhaps you'll get an IOException thrown if (for example) you try to delete a file, and there's a hardware error. Or a networking error.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

I'm pretty sure I've seen an IOException with Oracle JDK on Windows when the file was locked. I switched to using deleteOnExit() to get past that problem.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
    
    7
Javap on java.io.File on Oracle's standard JDK doesn't show any throws clause on delete() method. So there is no way it can throw IOException.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Paul Anilprem wrote:Javap on java.io.File on Oracle's standard JDK doesn't show any throws clause on delete() method. So there is no way it can throw IOException.

Hmm. Come to think of it, that might have IBM's JDK that I am remembering . Or maybe it was a RuntimeException and not an IOException. But I'm positive I got some exception on delete().
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Nick Widelec wrote:Hi,
Directly from this API http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete%28%29:

delete

public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.


(emphasis mine)

here is the source-code taken from here:

It does not seem to throw an IOException.
Am I missing something?


Read the quote again -- it is saying that "the Files class defines the delete method to throw an IOException". Note that there is an extra "s". It is the "Files" class, specifically, the "java.nio.file.Files" class that is being referred to here.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Excellent point Henry. I gave you a cow.

I guess I saw a RuntimeException from what I'm remembering.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3314
    
    7
Henry Wong wrote:
Nick Widelec wrote:Hi,
Directly from this API http://docs.oracle.com/javase/7/docs/api/java/io/File.html#delete%28%29:

delete

public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.


(emphasis mine)

here is the source-code taken from here:

It does not seem to throw an IOException.
Am I missing something?


Read the quote again -- it is saying that "the Files class defines the delete method to throw an IOException". Note that there is an extra "s". It is the "Files" class, specifically, the "java.nio.file.Files" class that is being referred to here.

Henry

Wow ...makes perfect sense. mystery solved
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does File.delete() throw an IOException?