同时捕获java异常FileNotFound和IOException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22196066/
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
Catching java exceptions FileNotFound and IOException at the same time
提问by drx
Is the FileNotFoundException somehow a "sub-exception" of the IOException?
FileNotFoundException 是否是 IOException 的“子异常”?
This is my code opening an input stream to a file at the given path:
这是我的代码在给定路径打开一个文件的输入流:
method(){
FileInputStream fs;
try {
fs = new FileInputStream(path);
//
fs.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
How come I can neglect the FileNotFound and just catch the IOException instead? Is the FNFException a part of the IOException?
为什么我可以忽略 FileNotFound 而只是捕获 IOException 呢?FNFException 是 IOException 的一部分吗?
Instead of catching the exceptions, what if I throw an IOException in my method?
如果我在我的方法中抛出一个 IOException ,而不是捕捉异常呢?
method() throws IOException{
FileInputStream fs;
fs = new FileInputStream(path);
//
fs.close();
}
Can I proceed to catch a FileNotFoundException at the invoking method like this?
我可以在这样的调用方法中继续捕获 FileNotFoundException 吗?
try {
method();
}catch (FileNotFoundException e1) {}
Thanks for any help you might be able to provide!
感谢您提供的任何帮助!
采纳答案by dasblinkenlight
Is the FileNotFoundException somehow a "sub-exception" of the IOException?
FileNotFoundException 是否是 IOException 的“子异常”?
Yes, FileNotFoundExceptionextendsIOException:
是的,扩展:FileNotFoundExceptionIOException
java.lang.Object java.lang.Throwable java.lang.Exception java.io.IOException java.io.FileNotFoundExceptionHow come I can neglect the FileNotFound and just catch the IOException instead?
java.lang.Object java.lang.Throwable java.lang.Exception java.io.IOException java.io.FileNotFoundException为什么我可以忽略 FileNotFound 而只是捕获 IOException 呢?
Catching a base class of the exception being thrown will catch the exception, unless there is a more specific catchclause available.
捕获所抛出异常的基类将捕获该异常,除非有更具体的catch子句可用。
Can I proceed to catch a FileNotFoundException at the invoking method like this?
我可以在这样的调用方法中继续捕获 FileNotFoundException 吗?
Absolutely! In fact, this is a good thing to do: your code should handle only the exceptions with which it knows how to deal, and let all other exceptions propagate to a place where it could be dealt with in a better way.
绝对地!事实上,这是一件好事:您的代码应该只处理它知道如何处理的异常,并让所有其他异常传播到可以以更好的方式处理的地方。
回答by Tim B
Yes, it is.
是的。
If you look at inheritance FileNotFoundExceptionis a sub-class of IOException. By catching the super class you also catch anything that extends it.
如果你看继承FileNotFoundException是IOException. 通过捕获超类,您还可以捕获任何扩展它的东西。
You can catch the more specific one first as in your first example if you need to handle it differently.
如果您需要以不同的方式处理它,您可以像在第一个示例中一样首先捕获更具体的一个。
回答by Pierre Rust
Yes, as the javadocshows it, FileNotFoundExceptionis a subclass of IOException.
是的,正如javadoc所示,它FileNotFoundException是IOException.
If you really wantFileNotFoundException, you must catch only this execption, otherwise catching IOExceptionwill also catch any exception subclassing it, like FileNotFoundExceptionany many others.
如果你真的想要FileNotFoundException,你必须只捕获这个 execption,否则捕获IOException也会捕获任何子类化它的异常,就像FileNotFoundException许多其他异常一样。
回答by Martin Dinov
From Java 7 onwards you can do:
从 Java 7 开始,您可以执行以下操作:
catch(ExceptionType1 | ExceptionType2 e)to do multiple-exception catches. However, in your case, you can just catch the IOException, as you suggest.
catch(ExceptionType1 | ExceptionType2 e)进行多异常捕获。但是,在您的情况下,您可以按照您的建议捕获 IOException。
回答by fge
Is the FileNotFoundException somehow a "sub-exception" of the IOException?
FileNotFoundException 是否是 IOException 的“子异常”?
是的。
Instead of catching the exceptions, what if I throw an IOException in my method?
如果我在我的方法中抛出一个 IOException ,而不是捕捉异常呢?
You can do so. When a method throws an excpetion, it can throw this exception in particular or any exception inheriting it.
你可以这样做。当一个方法抛出一个异常时,它可以特别抛出这个异常或任何继承它的异常。
Can I proceed to catch a FileNotFoundException at the invoking method like this?
我可以在这样的调用方法中继续捕获 FileNotFoundException 吗?
Yes. If a method declares to throw A, you can catch Bif Binherits A(even if Bisn't thrown by Aexplicitly).
是的。如果方法声明为 throw A,则可以捕获BifB继承A(即使B未A显式抛出)。
Side note:
边注:
- you don't close your resources correctly;
- if you use Java 7, use the new Files API instead.
- 你没有正确关闭你的资源;
- 如果您使用 Java 7,请改用新的 Files API。
The first point is very important. You .close()in your tryblock; if you can successfully open the file but fail to read from it or whatever, .close()will notbe called. You should do:
第一点非常重要。你.close()在你的try街区;如果您可以成功打开文件但无法读取文件或其他任何内容,.close()则不会被调用。你应该做:
FileInputStream in = ...;
try {
// operate on "in"
} finally {
in.close();
}
For Java 7:
对于 Java 7:
try (
FileInputStream in = ...;
) {
// operate on "in"
}
// "in" is closed for you here

