java 未处理的异常类型 IOException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13957961/
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
Unhandled Exception Type IOException
提问by Bennett
Possible Duplicate:
Why do I get the “Unhandled exception type IOException”?
I'm trying to solve Euler #8 using the following algorithm. Problem is, whenver I modify the line I have the giant comment on, the error Unhandled Exception Type IOException
appears on each line that I've marked with the comment //###
.
我正在尝试使用以下算法解决 Euler #8。问题是,每当我修改有大量注释的行时,错误都会Unhandled Exception Type IOException
出现在我用注释标记的每一行上//###
。
private static void euler8()
{
int c =0;
int b;
ArrayList<Integer> bar = new ArrayList<Integer>(0);
File infile = new File("euler8.txt");
BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(infile), //###
Charset.forName("UTF-8")));
while((c = reader.read()) != -1) { //###
char character = (char) c;
b = (int)character;
bar.add(b); /*When I add this line*/
}
reader.close(); //###
}
回答by Jon Skeet
Yes, IOException
is a checked exception, which means you either need to catch it, or declare that your method will throw it too. What do you wantto happen if the exception is thrown?
是的,IOException
是一个已检查的异常,这意味着您要么需要捕获它,要么声明您的方法也会抛出它。如果抛出异常,你希望发生什么?
Note that you should generally be closing the reader
in a finally
block anyway, so that it gets closed even in the face of another exception.
请注意,您一般应封闭reader
在一个finally
块反正,以便它能够在其他的异常的脸关闭。
See the Java Tutorial lesson on exceptionsfor more details about checked and unchecked exceptions.
回答by AlexWien
One solution: change to
一种解决方案:更改为
private static void euler8() throws IOException {
But then the calling method has to catch the IOException.
但是随后调用方法必须捕获 IOException。
or catch the Exception:
或捕获异常:
private static void euler8()
{
int c =0;
int b;
ArrayList<Integer> bar = new ArrayList<Integer>(0);
BufferedReader reader;
try {
File inFile = new File("euler8.txt");
reader = new BufferedReader(
new InputStreamReader(
new FileInputStream(infile), //###
Charset.forName("UTF-8")));
while((c = reader.read()) != -1) { //###
char character = (char) c;
b = (int)character;
bar.add(b); /*When I add this line*/
}
} catch (IOException ex) {
// LOG or output exception
System.out.println(ex);
} finally {
try {
reader.close(); //###
} catch (IOException ignored) {}
}
}
回答by Naftali aka Neal
Wrap in a try/catch block to catch the Exceptions.
包裹在 try/catch 块中以捕获异常。
If you do not do that it will go unhandled.
如果您不这样做,它将无法处理。
回答by Brian Agnew
What happens if you can't read the nominated file ? The FileInputStream
will throw an exception and Java mandates that you'll have to check for this and handle it.
如果您无法读取指定文件会怎样?该FileInputStream
会抛出一个异常,和Java的任务,你必须检查这一点,并处理它。
This type of exception is called a checkedexception. Uncheckedexceptions exist and Java doesn't require you to handle these (largely because they're unhandable - e.g. OutOfMemoryException
)
这种类型的异常称为检查异常。存在未经检查的异常,Java 不需要您处理这些异常(主要是因为它们无法处理 - 例如OutOfMemoryException
)
Note that your handling may include catching it and ignoring it. This isn't a good idea, but Java can't really determine that :-)
请注意,您的处理可能包括抓住它并忽略它。这不是一个好主意,但 Java 无法真正确定:-)