鉴于以下片段,指定失败的原因或抛出IOException是明智的吗?
我正在创建一个“文件经理”来练习我的IO技能。 如果要创建一个文件,如果文件#createNewfile()返回false?现在,我将如何处理错误?
@Override
public void run() {
File file = new File(src + "\\" + srcName);
try {
if (file.createNewFile()) {
MessageHandler.createSuccess(comp);
} else {
throw new IOException();
}
} catch (IOException e) {
MessageHandler.error500(comp);
}
}
现在,我只是扔了一个ioexception,以捕获下一个块,但是我'我不确定这是否是最好的主意。在客户端应该很好,但是当调试时,我不知道内部出了什么问题。
I'm creating a "File Manager" to practice my IO skills. If I'm creating a file, how would I handle an error if file#createNewFile() returns false?
@Override
public void run() {
File file = new File(src + "\\" + srcName);
try {
if (file.createNewFile()) {
MessageHandler.createSuccess(comp);
} else {
throw new IOException();
}
} catch (IOException e) {
MessageHandler.error500(comp);
}
}
Right now I'm simply throwing an IOException to be caught in the next block, but I'm not sure if that is the best idea. It should be fine on the client end but when debugging I would have no idea what went wrong internally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于我想没有一个明确的答案,因此社区的其他成员可能对此有所不同。
根据 javadoc ,
createNewfile
仅在文件已经存在时返回false。因此,如果不应该存在文件的情况,我也只扔一个
ioexception
。如果我有理由假设文件可能存在,则应以某种方式将其与程序进行交互的用户相互作用。您的工作方式确切取决于您程序的UI。
It depends. I guess there is no single clear answer to this, so other members of the community might view this differently than I do.
According to the Javadoc,
createNewFile
returns false only if the file already exists.Therefore, in cases where the file should not be present whatsoever, I also just throw an
IOException
.If I have a reason to assume the file could be present, the user interacting with the program should be notified of this problem in some way. How exactly you do this depends on the UI of your program.
您向我们展示的代码片段没有多大意义。抛出一个例外,只是为了立即抓住它,不是很好的表现(除非聪明的Java优化器消除了昂贵的操作)。
您的当前运行()方法确实:
MessageHandler.CreateSuccess()
如果文件不存在并且可以创建(file.createnewfile()
返回/代码>)。MessageHandler.Error500()
如果文件不存在但不能创建,例如,因为没有必要的权限(file.createnewfile()
投掷and <代码> ioexception )。MessageHandler.Error500()
如果文件已经存在(file> file.createnewfile()
返回true
)。 [您确定已经有文件是错误的吗? >捕获子句)。
我只是简单地将代码重写为类似的内容,
甚至可能找到与500不同的结果代码,更好地表示失败的原因。
例外是一种很好的方法,可以使一种方法与其他方法交流其呼叫者层次结构,无法完成工作。在一种方法中投掷并捕捉例外很少是一个好主意。
The code snippet you showed us does not make much sense. Throwing an exception, just to catch it immediately, isn't very performant (unless the clever Java optimizers eliminate the costly operations).
Your current run() method does:
MessageHandler.createSuccess()
if the file didn't exist and could be created (file.createNewFile()
returnedtrue
).MessageHandler.error500()
if the file didn't exist, but could not be created, e.g. because of not having the necessary permissions (file.createNewFile()
threw anIOException
).MessageHandler.error500()
if the file already existed (file.createNewFile()
returnedtrue
). [Are you sure that already having the file is an error?]For the case number 3, creating, throwing and catching an
IOException
does not serve any useful purpose (other than guiding the code into thecatch
clause).I'd simply rewrite the code to something like
You might even find a result code different from 500, better representing the cause of the failure.
Exceptions are a very good way for one method to communicate to the other methods up its caller hierarchy that it wasn't able to do its job. Throwing and catching an exception within one method rarely is a good idea.