Java 为什么我会收到“未处理的异常类型 IOException”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2305966/
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
Why do I get the "Unhandled exception type IOException"?
提问by Roman
I have the following simple code:
我有以下简单的代码:
import java.io.*;
class IO {
public static void main(String[] args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
}
}
And I get the following error message:
我收到以下错误消息:
----------
1. ERROR in io.java (at line 10)
while ((userInput = stdIn.readLine()) != null) {
^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java
Does anybody have any ideas why? I just tried to simplify the code given on the sum web site (here). Did I oversimplify?
有没有人知道为什么?我只是试图简化 sum 网站上给出的代码(这里)。我是否过于简单化了?
采纳答案by Marcin
You should add "throws IOException" to your main method:
您应该在主方法中添加“throws IOException”:
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException {
You can read a bit more about checked exceptions (which are specific to Java) in JLS.
您可以在JLS 中阅读更多有关已检查异常(特定于 Java)的信息。
回答by jkff
Java has a feature called "checked exceptions". That means that there are certain kinds of exceptions, namely those that subclass Exception but not RuntimeException, such that if a method may throw them, it mustlist them in its throws declaration, say: void readData() throws IOException. IOException is one of those.
Java 有一个叫做“检查异常”的特性。这意味着存在某些类型的异常,即那些子类为 Exception 而不是 RuntimeException 的异常,这样如果一个方法可能抛出它们,它必须在它的 throws 声明中列出它们,比如:void readData() throws IOException。IOException 就是其中之一。
Thus, when you are calling a method that lists IOException in its throws declaration, you must either list it in your own throws declaration or catch it.
因此,当您调用在 throws 声明中列出 IOException 的方法时,您必须在自己的 throws 声明中列出它或捕获它。
The rationale for the presence of checked exceptions is that for some kinds of exceptions, you must not ignore the fact that they may happen, because their happening is quite a regular situation, not a program error. So, the compiler helps you not to forget about the possibility of such an exception being raised and requires you to handle it in some way.
存在已检查异常的基本原理是,对于某些类型的异常,您不能忽略它们可能发生的事实,因为它们的发生是很正常的情况,而不是程序错误。因此,编译器帮助您不要忘记引发此类异常的可能性,并要求您以某种方式处理它。
However, not all checked exception classes in Java standard library fit under this rationale, but that's a totally different topic.
但是,并非 Java 标准库中的所有已检查异常类都符合此基本原理,但这是一个完全不同的主题。
回答by Parth
Try again with this code snippet:
使用以下代码片段重试:
import java.io.*;
class IO {
public static void main(String[] args) {
try {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
} catch(IOException ie) {
ie.printStackTrace();
}
}
}
Using try-catch-finally
is better than using throws
. Finding errors and debugging are easier when you use try-catch-finally
.
使用try-catch-finally
比使用更好throws
。使用try-catch-finally
.
回答by vivz
Reading input from keyboard is analogous to downloading files from the internet, the java io system opens connections with the source of data to be read using InputStream or Reader, you have to handle a situation where the connection can break by using IOExceptions
从键盘读取输入类似于从 Internet 下载文件,java io 系统使用 InputStream 或 Reader 打开与要读取的数据源的连接,您必须处理使用 IOExceptions 断开连接的情况
If you want to know exactly what it means to work with InputStreams and BufferedReader this videoshows it
如果你想确切地知道使用 InputStreams 和 BufferedReader 意味着什么,这个视频会展示它
回答by harun ugur
add "throws IOException" to your method like this:
将“throws IOException”添加到您的方法中,如下所示:
public static void main(String args[]) throws IOException{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
}