Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does File.delete() throw an IOException?

 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3760
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21314
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3760
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3760
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic