JDK-4505804 : File.listFiles() requires null check on return value - should have better error handling
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.3.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-09-21
  • Updated: 2009-02-16
  • Resolved: 2009-02-16
Related Reports
Duplicate :  
Name: gm110360			Date: 09/21/2001

java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

The various listFiles methods of File all have the following annoying feature:
they may possibly return null if an I/O error occurs.

This behavior of listFiles is very inconvenient, because it means that not only
must I have code that handles IOExceptions (which is fine), but I also have to
do additional null checks on the result.

Better behavior would be that if an I/O error occurs, than an IOException always
gets thrown.  You should not use a distinguished return value to sometimes
indicate errors, and then also other times throw Exceptions!

(Note: the other time that null can be retuned from listFiles is if the path is
not a directory.  It would probably be better behavior to always thru an
Exception in this case too instead of returning null.)
(Review ID: 132377) 

EVALUATION We plan to address this longstanding problem in the forthcoming new filesystem API. -- ###@###.### 2002/3/21