JDK-4952996 : File.delete() should throw IOException instead of returning false
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2003-11-12
  • Updated: 2009-02-16
  • Resolved: 2009-02-16
Related Reports
Duplicate :  
Description
Name: rmT116609			Date: 11/12/2003


A DESCRIPTION OF THE REQUEST :
if java.io.File.delete() fails, the result value is false. there is no further information about the reason why it failed. it would be better that this method would throw a IOException with the reason why it failed. (in my case, it failed because another process still had a reference to the file.)

JUSTIFICATION :
with the current behaviour (just return false if delete() failed) there is no information why the deletion failed.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
e.g. if the file can't be deleted because it's still in access by another process, don't just return false but throw an IOException with the reason (e.g. "File is still referenced by another process.")
ACTUAL -
the method just returns false.

---------- BEGIN SOURCE ----------
File file = new File("readme.txt");
boolean result = file.delete();
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
you have to guess the reason why delete() failed.
(Incident Review ID: 225294) 
======================================================================

Comments
EVALUATION This feature has been addressed by the new file system API defined by JSR-203. In particular, for a File f then f.toPath().delete() will throw an IOException if the delete fails.
16-02-2009

EVALUATION It is not possible to change delete to throw IOException in this case because that would be incompatible. It would be possible to offer some other method that does throw under these circumstances, either in File or some new file handling facility. ###@###.### 2003-11-12
12-11-2003