[ 
https://issues.apache.org/jira/browse/IO-813?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17791046#comment-17791046
 ] 

Elliotte Rusty Harold edited comment on IO-813 at 11/29/23 12:25 PM:
---------------------------------------------------------------------

Not only does it not enforce the constraint that x.compareTo( y ) must throw an 
exception iff y.compareTo( x ) throws an exception. It does not even enforce 
the constraint that x.compareTo( y ) must throw an exception iff x.compareTo( y 
) throws an exception. That is if you call lastModified twice on the same 
object, one call might throw an IOException while the other does not. I/O is 
not a deterministic operation.

It's not likely to fail, of course, but it can and does. That this is a rare 
occurrence males the problem worse. This can be completely missed in testing 
until it happens unexpectedly in production. That's why IOExceptions really 
need to be checked. It is unsafe to simply convert an IOException into an 
unchecked exception. 




was (Author: elharo):
Not only does it not enforce the constraint that x.compareTo( y ) must throw an 
exception iff y.compareTo( x ) throws an exception. It does not even enforce 
the constraint that x.compareTo( y ) must throw an exception iff x.compareTo( y 
) throws an exception. That is if you call lastModified twice on the same 
object one call might throw an IOException while the other does not. I/O is not 
a deterministic operation.

It's not likely to fail, of course, but it can and does. That this is a rare 
occurrence males the problem worse. This can be completely missed in testing 
until it happens unexpectedly in production. That's why IOExceptions really 
need to be checked. It is unsafe to simply convert an IOException into an 
unchecked exception. 



> 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)

Reply via email to