FileNotFoundException与NoSuchFileException异常 [英] FileNotFoundException vs. NoSuchFileException
问题描述
我注意到另一个Java异常表示该文件不存在 - NoSuchFileException
。我的任务是重构一些api,它们都是从不同的方法中抛出的,我只想使用一个。
I noticed another Java exception for indicating that file does not exist - NoSuchFileException
. I was tasked to refactor a certain api which throws both of these from differen methods and I would like to use just one.
我应该将 NoSuchFileException
映射到 FileNotFoundException
?我应该使用 NoSuchFileException
而不是 FileNotFoudnException
,因为它更具体?
Should I map NoSuchFileException
to file to FileNotFoundException
? Should I use NoSuchFileException
instead of FileNotFoudnException
because it is more specific?
编辑:更新了问题。在发布此问题之前,我阅读了文档,并了解了基本的区别。我希望在这种情况下提供更多信息和指导,因为类型的异常处理对于服务api的客户端很重要,我想避免在需要为两种异常类型执行检查的情况下。
Updated the question. I read the documentation before posting this question and know the basic difference. I was hoping for additional information and the guidance in this case since exception handling by type is important for the clients of the service api and I would like to avoid the case when the check needs to be done for both exception types.
推荐答案
表示尝试打开由指定路径名表示的文件失败。
这个异常将由FileInputStream
,FileOutputStream
和RandomAccessFile
当具有指定路径名的文件不存在时,构造函数。 如果文件确实存在但由于某种原因存在,这些构造函数也将被抛出,例如尝试打开只读文件进行写入时。
Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the
FileInputStream
,FileOutputStream
, andRandomAccessFile
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.
当尝试访问不存在的文件时,检查的异常抛出。 p>
Checked exception thrown when an attempt is made to access a file that does not exist.
文档不言自明。
这篇关于FileNotFoundException与NoSuchFileException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!