You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Elliotte Rusty Harold (Jira)" <ji...@apache.org> on 2023/10/01 21:31:00 UTC
[jira] [Comment Edited] (IO-813) LastModifiedFileComparator should not throw exceptions, period
[ https://issues.apache.org/jira/browse/IO-813?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17770928#comment-17770928 ]
Elliotte Rusty Harold edited comment on IO-813 at 10/1/23 9:30 PM:
-------------------------------------------------------------------
Throwing an exception is not a problem. Throwing an exception from Comparable.compareTo is a big problem, and throwing one not allowed by the contract of Comparable.compareTo is a huge problem. When LastModifiedFileComparator is used in Collections.sort or equivalent it can leave objects in inconsistent states.
Comparable should not be used in scenarios where I/O errors are a possibility. You need something that's designed to include the possibility of an I/O error. Converting to a runtime exception does not solve the problem. It simply pretends the problem won't happen.
was (Author: elharo):
Throwing an exception is not a problem. Throwing an exception from Comparable.compareTo is a big problem, and throwing one not allowed by the contract of Comparable.compareTo is a huge problem. When LastModifiedFileComparator is used in Collections.sort or equivalent it can leave objects in inconsistent states.
Comparable should not be used in scenarios where I/O errors are a possibility. You need something that's planned around the possibility if an I/O error. Converting to a runtime exception does not solve the problem. It simply pretends the problem won't happen.
> 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)