[
https://issues.apache.org/jira/browse/IO-813?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17791779#comment-17791779
]
Elliotte Rusty Harold edited comment on IO-813 at 11/30/23 8:57 PM:
--------------------------------------------------------------------
Interesting. The Javadoc doesn't show IOException for File.lastModified, which
is strange but this goes back to Java 1.0. Instead it returns 0 on I/O error,
though that is still potentially inconsistent since this method accesses the
file system. It is not simply returning a value from the object. Accesses to
the file system are not reliable. Both race conditions and outright failures
can happen here.
However this comparator doesn't call File.lastModified() (at least not at head,
perhaps it used to). Instead tt calls java.nio.file.Files.getLastModifiedTime
and that method does throw an IOException when something goes wrong here.
was (Author: elharo):
Interesting. The Javadoc doesn't show IOException for File.lastModified, which
is strange but this goes back to Java 1.0. Instead it returns 0 on I/O error,
though that is still potentially inconsistent since this method accesses the
file system. It is not simply returning a value from the object. Accesses to
the file system are not reliable. Both race conditions and outright failures
can happen here.
However this comparator doesn't call File.lastModified(). It calls
Files.getLastModifiedTime and that method does throw an IOException when
something goes wrong here.
> LastModifiedFileComparator should not throw exceptions, period
> --------------------------------------------------------------
>
> Key: IO-813
> URL: https://issues.apache.org/jira/browse/IO-813
> Project: Commons IO
> Issue Type: Bug
> Reporter: Elliotte Rusty Harold
> Priority: Major
>
> LastModifiedFileComparator is likely broken by design since it can
> unexpectedly throw UncheckedIOException. This violates the contract of
> Comparable.compareTo which is not documented to throw that exception.
> I analyzed almost this exact case in detail here:
> https://medium.com/@elharo/when-you-cant-throw-an-exception-b9f9b0db9ba4
> I'm not sure how to fix this now, but I'm tempted to simply deprecate this
> entire class.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)