java 为什么 FileNotFoundException 是 CheckedException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28896120/
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 FileNotFoundException is CheckedException?
提问by Lathy
I know FileNotFound is Checked Exception but though it is, only during the Run time this exception will occur.It is more like Arithmetic Exception(Unchecked).
我知道 FileNotFound 是 Checked Exception 但尽管如此,但只有在运行时才会发生此异常。它更像是算术异常(未检查)。
Whether it is checked or unchecked the exception will happen only during runtime.
无论是检查还是未检查,异常只会在运行时发生。
My Question is why we call the FileNotFound/IO/DB
related stuffs as Checked Exception?
我的问题是为什么我们称FileNotFound/IO/DB
相关的东西为检查异常?
Please share me your valuable thoughts :)
请与我分享您的宝贵意见:)
回答by Kavan
Exceptions always encountered at runtime only, Difference is made when a exception is handled.
异常总是只在运行时遇到,处理异常时就会产生差异。
Checked or unchecked means whether it is forced to handle at compile time or it will only be identified when it is encountered at runtime.
Checked 或 unchecked 表示是在编译时强制处理还是只有在运行时遇到时才会识别。
If an exception is checked means compiler has way to identify whether the exception can occur or not. and whenever you compile it, you will be forced to handle a checked exception and by doing this chances of runtime exceptions will be reduced.
如果检查异常意味着编译器有办法识别异常是否会发生。并且无论何时编译它,您都将被迫处理已检查的异常,并且通过这样做会减少运行时异常的机会。
During file handling, compiler doesn't check whether file is present or not, it just check whether you have handled fileNotFoundException or not, because once you are dealing with a file chances of encountering this exception is very high and you should handle it in your code. for arithmetic Exception there is not way to find it during compile time. and thus it is left unchecked.
在文件处理过程中,编译器不检查文件是否存在,它只检查你是否处理过 fileNotFoundException ,因为一旦你正在处理一个文件,遇到这个异常的机会非常高,你应该在你的代码。对于算术异常,在编译时无法找到它。因此它没有被检查。
回答by maraca
NullPointerException
or ArithmeticException
usually shouldn't happen in a finished, correct program. You can handle those just with checking before with if
to see if you divide by 0
or an object is null
and then you are sure this Exception will not be thrown. Every time handling those Exceptions can make the code less readable.
NullPointerException
或者ArithmeticException
通常不应该发生在一个完成的、正确的程序中。您可以通过在 with 之前检查来处理这些问题,if
以查看您是否被除以0
或一个对象是null
,然后您确定不会抛出此异常。每次处理这些异常都会降低代码的可读性。
Now you could argue that you could do the same for FileNotFoundException
by just checking if the file exists before doing anything. But many constructors or methods that expect a File
also support String
from which the file is created then. I guess it is a question of where you draw the line, if you always only have the File
method and never also support String
then I would have added it to the unchecked ones too I think.
现在你可以争辩说你可以FileNotFoundException
通过在做任何事情之前检查文件是否存在来做同样的事情。但是许多构造函数或方法也希望File
支持String
创建文件。我想这是你在哪里划线的问题,如果你总是只有File
方法并且从不支持,String
那么我也会将它添加到未经检查的方法中,我认为。
In other words: if a FileNotFoundException
is thrown then it can be the desired behaviour and drive the flow of your program, but NullPoinerException
really shouldn't be used for that.
换句话说:如果 aFileNotFoundException
被抛出,那么它可以是所需的行为并驱动程序的流程,但NullPoinerException
实际上不应该用于那个。
回答by Svetlin Zarev
All exceptions can happen only during runtime :) The difference between the Checked
and Unchecked
exceptions is, that the compiler is forcing you to handle the checked
ones or add them to the method signature, effectively forcing the caller to do the same (handle/rethrow).
所有异常只能在运行时发生:)Checked
和Unchecked
异常之间的区别在于,编译器强制您处理这些异常checked
或将它们添加到方法签名中,从而有效地强制调用者执行相同的操作(处理/重新抛出)。
回答by isah
They've let it be a Checked Exception because the user can possibly "recover" from this exception by handling it. For example, the user may specify a different directory in case this exception happened.
他们让它成为一个检查异常,因为用户可以通过处理它从这个异常中“恢复”。例如,万一发生这种异常,用户可以指定不同的目录。
回答by Saum
Checked Exceptions force users to explicitly handle them, they are used for 'recoverable' exceptions, where the user can handle the situation gracefully.
Checked Exceptions 强制用户明确处理它们,它们用于“可恢复”异常,用户可以优雅地处理这种情况。
Let's take a FileNotFound
- it is typically thrown when the file is not present and below is a related programming idiom:
让我们来看看FileNotFound
- 它通常在文件不存在时抛出,下面是相关的编程习惯用法:
FileInputStream fis = null;
try {
fis = new FileInputStream(new File(""));
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
The Checked Exception here forces me to declare it in a try/catch block, where I can close the fis
gracefully even if there's an exception.
这里的 Checked Exception 迫使我在 try/catch 块中声明它,fis
即使有异常,我也可以优雅地关闭它。
Now consider that FileNotFound
is a runtime exception, the code hypothetically will look like below:
现在考虑这FileNotFound
是一个运行时异常,代码假设如下所示:
FileInputStream fis = null;
fis = new FileInputStream(new File(""));
fis.close();
Now if this throws a runtime exception - which you do not need to handle at compile time, your fis
will not be closed gracefully and that is a resource leak.
现在,如果这会引发运行时异常 - 您不需要在编译时处理该异常,您fis
将不会正常关闭,这是资源泄漏。