gpt4 book ai didi

java - 当 File.listFiles 返回 null 时检索底层错误

转载 作者:太空狗 更新时间:2023-10-29 22:53:51 31 4
gpt4 key购买 nike

根据File.listFiles javadoc方法

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

我知道我正在使用一个目录,但收到了一个 null 结果,所以一定是出现了 I/O 错误。我对错误是什么非常感兴趣。

当返回这样的 null 结果时,如何检索错误消息/代码?

最佳答案

你不能。不幸的是,该 API 没有为您提供找出底层 I/O 错误的方法。

另见

  • Bug 4505804: File.listFiles() requires null check on return value - should have better error handling

    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 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, then an IOException always gets thrown. You should not use a distinguished return value to sometimes indicate errors, and then also other times throw Exceptions!

    Evaluation: We plan to address this longstanding problem in the forthcoming new filesystem API.

关于java - 当 File.listFiles 返回 null 时检索底层错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3228147/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com