Java 当 IOException 通常发生时,我应该采取什么措施来正确处理它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19616263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When an IOException generally occur and what action should I take to properly handle it?
提问by Winn
I am using File Input/Output streams. I know that reading a non-existent file from program using FileInputStream
will give FileNotFoundException
. Right? So I can catch this excpetion and can return null or 0 value(depends on return type of my method performing all this reading operations) on Exception to convey the calling program that file does not exist and should create the one.
我正在使用文件输入/输出流。我知道从程序中读取一个不存在的文件使用FileInputStream
会给FileNotFoundException
. 对?所以我可以捕获这个异常并且可以在 Exception 上返回 null 或 0 值(取决于我执行所有这些读取操作的方法的返回类型)来传达调用程序文件不存在并且应该创建一个。
But I don't know when IOException
generally occurs and what is the exact reason I should convey to calling program that 'this' has happened because of 'that'.
I exactly don't know what is 'this' and 'that' here.
但我不知道IOException
一般什么时候发生,我应该向调用程序传达“这个”是因为“那个”而发生的确切原因是什么。我完全不知道这里的“这个”和“那个”是什么。
Anybody please elaborate me in what cases does the IOException
can occur and what specific action should I take in such case.
Please help. Thanks.
任何人都请详细说明在什么情况下IOException
会发生这种情况以及在这种情况下我应该采取什么具体行动。请帮忙。谢谢。
采纳答案by Ankit Rustagi
What is an IOException
什么是 IOException
An IOException
is any unexpected problem the JVM encounters while attempting to run a program. Possible problems that it may encounter are:
AnIOException
是 JVM 在尝试运行程序时遇到的任何意外问题。它可能遇到的问题有:
- attempting to read from a file that does not exist
- attempting to write to a file that has an invalid name (a slash or a question mark in the title should do it)
- attempting to read the next token in a file when there are no more tokens.
- 试图从不存在的文件中读取
- 尝试写入具有无效名称的文件(标题中的斜杠或问号应该这样做)
- 当没有更多标记时尝试读取文件中的下一个标记。
When an IOException
is thrown, it means that whatever is throwing the exception (perhaps a try{}-catch
block that reads data from a file) can throw an IOException
, for example if the file is not found, corrupted, etc, or when the file is otherwise unable to be read, or any other of a list of issues that can occur with the IO
package and it's extensions.
当 anIOException
被抛出时,这意味着任何抛出异常的东西(可能是一个try{}-catch
从文件读取数据的块)都可以抛出一个IOException
,例如,如果文件未找到、损坏等,或者当文件以其他方式无法被阅读,或IO
包及其扩展可能出现的任何其他问题列表。
What to do when you encounter an IOException
?
遇到了IOException
怎么办?
When you encounter the IOException
, you could log it or print an error message. If you are reading from a file that does not exit, you could create one to avoid future exceptions. A lot depends on what you are doing. If you are debugging, printing the stacktrace is always helpful.
当您遇到 时IOException
,您可以记录它或打印错误消息。如果您正在读取未退出的文件,则可以创建一个文件以避免将来出现异常。很大程度上取决于你在做什么。如果您正在调试,打印堆栈跟踪总是有帮助的。
Refer to the javadoc
参考javadoc
回答by Maroun
It is usually when file doesn't exist or you don't have the privileges to read/write.. etc..
通常是在文件不存在或您没有读/写权限时......等等。
Could be other things of course, so the best thing you can do, is printing the messageand see what caused the exception. Furthermore, you can see on each class, what methods throws what and when, I advise you to investigate that, it might help you to cover more cases on which you might face this exception.
当然可能是其他事情,所以你能做的最好的事情就是打印消息并查看导致异常的原因。此外,您可以在每个类上看到什么方法抛出什么以及何时抛出,我建议您调查一下,它可能会帮助您涵盖更多可能面临此异常的情况。
What should you do when you have an exception? It's up to you an your program, no specific answer for that, depends on many things.
遇到异常怎么办?这取决于你的程序,没有具体的答案,取决于很多事情。
回答by MouseLearnJava
You can do the following things:
您可以执行以下操作:
Log the Exception informationon a log file. You can use the following method to populate the Exception information.
Try to close the InputStream/OutputStream if it is not null when the IOECeption happens in reading / writing.
Throw an Exception of your own with meaningful messageto let user know what happens.
在日志文件中记录异常信息。您可以使用以下方法填充异常信息。
如果在读/写时发生 IOECeption 时 InputStream/OutputStream 不为 null,则尝试关闭 InputStream/OutputStream。
用有意义的消息抛出你自己的异常,让用户知道发生了什么。