gpt4 book ai didi

java - FileNotFoundException 与 NoSuchFileException

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:36 25 4
gpt4 key购买 nike

我注意到另一个指示文件不存在的 Java 异常 - NoSuchFileException。我的任务是重构某个 api,它从不同的方法中抛出这两个,我只想使用一个。

我应该将 NoSuchFileException 映射到文件到 FileNotFoundException 吗?我应该使用 NoSuchFileException 而不是 FileNotFoudnException 因为它更具体吗?

编辑:更新了问题。我在发布此问题之前阅读了文档并了解了基本区别。我希望在这种情况下获得更多信息和指导,因为按类型进行异常处理对于服务 api 的客户端很重要,我希望避免需要对两种异常类型进行检查的情况。

最佳答案

FileNotFoundException

Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

NoSuchFileException

Checked exception thrown when an attempt is made to access a file that does not exist.

文档是不言自明的。

关于java - FileNotFoundException 与 NoSuchFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424237/

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