IOException and FileVisitor

Alan Bateman Alan.Bateman at Sun.COM
Mon Feb 8 07:28:17 PST 2010


Rémi Forax wrote:
> Hi Alan, hi all,
> I don't understand why method visit* and postVisit of a FileVsitor 
> doesn't propagate IOException ?
> I've written a program one week ago that use a SimpleFileVisitor and I 
> get only now an IOError
> (visitFileFailed throws that error)
> The current design is not very robust.
>
> I prefer a crystal clear contract that explicitly say, well be 
> prepared to catch some IO exception
> while walking a file tree.
>
> Rémi
>
Thanks for bringing this up. There are many ways to deal with I/O 
exceptions when doing recursive operations. Command-line tools and 
utilities will often just print a warning to standard error and 
continue. In other cases you might implement a FileVisitor that 
accumulates all I/O exceptions encountered during the walk. Another 
approach is to terminate the walk when an I/O exception is encountered 
something this can done gracefully (by returning TERMINATE) or by 
throwing an exception that is propagated to the caller of 
Files.walkFileTree. Looking at it now I think you are right and 
FileVisitor's method should declare IOException. That would avoid 
SimpleFileVisitor wrapping the exception as an IOError as it does now.

-Alan.


More information about the nio-dev mailing list