Java 如何捕获将通过读取和写入文件抛出的所有异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1075895/
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
How can I catch all the exceptions that will be thrown through reading and writing a file?
提问by Johanna
In Java, is there any way to get(catch) all exceptions
instead of catch the exception individually?
在 Java 中,有没有办法获取(捕获)全部exceptions
而不是单独捕获异常?
采纳答案by jjnguy
If you want, you can add throws clauses to your methods. Then you don't have to catch checked methods right away. That way, you can catch the exceptions
later (perhaps at the same time as other exceptions
).
如果需要,您可以在方法中添加 throws 子句。然后您不必立即捕获已检查的方法。这样,您就可以捕获exceptions
后者(可能与 other 同时捕获exceptions
)。
The code looks like:
代码如下:
public void someMethode() throws SomeCheckedException {
// code
}
Then later you can deal with the exceptions
if you don't wanna deal with them in that method.
然后,exceptions
如果您不想在该方法中处理它们,则可以处理它们。
To catch all exceptions some block of code may throw you can do: (This will also catch Exceptions
you wrote yourself)
要捕获所有异常,您可以执行一些代码块可能抛出的异常:(这也将捕获Exceptions
您自己编写的)
try {
// exceptional block of code ...
// ...
} catch (Exception e){
// Deal with e as you please.
//e may be any type of exception at all.
}
The reason that works is because Exception
is the base class for all exceptions. Thus any exception that may get thrown is an Exception
(Uppercase 'E').
有效的原因是因为它Exception
是所有异常的基类。因此,可能抛出的任何异常都是Exception
(大写的“E”)。
If you want to handle your own exceptions first simply add a catch
block before the generic Exception one.
如果您想首先处理自己的异常,只需catch
在通用异常之前添加一个块。
try{
}catch(MyOwnException me){
}catch(Exception e){
}
回答by Allan
Catch the base exception 'Exception'
捕获基本异常“异常”
try {
//some code
} catch (Exception e) {
//catches exception and all subclasses
}
回答by JoshJordan
Do you mean catch an Exception
of any typethat is thrown, as opposed to just specific Exceptions?
您的意思是捕获抛出Exception
的任何类型,而不仅仅是特定的异常?
If so:
如果是这样的话:
try {
//...file IO...
} catch(Exception e) {
//...do stuff with e, such as check its type or log it...
}
回答by CodeFusionMobile
Yes there is.
就在这里。
try
{
//Read/write file
}catch(Exception ex)
{
//catches all exceptions extended from Exception (which is everything)
}
回答by Alex Feinman
It is bad practice to catch Exception-- it's just too broad, and you may miss something like a NullPointerExceptionin your own code.
捕获Exception是不好的做法——它太宽泛了,你可能会在你自己的代码中错过像NullPointerException这样的东西。
For most file operations, IOExceptionis the root exception. Better to catch that, instead.
对于大多数文件操作,IOException是根异常。最好抓住那个,而不是。
回答by codethulhu
While I agree it's not good style to catch a raw Exception, there are ways of handling exceptions which provide for superior logging, and the ability to handle the unexpected. Since you are in an exceptional state, you are probably more interested in getting good information than in response time, so instanceof performance shouldn't be a big hit.
虽然我同意捕获原始异常不是一种好风格,但有一些处理异常的方法可以提供卓越的日志记录和处理意外情况的能力。由于您处于特殊状态,您可能对获得好的信息比响应时间更感兴趣,因此 instanceof 性能不应该是一个大问题。
try{
// IO code
} catch (Exception e){
if(e instanceof IOException){
// handle this exception type
} else if (e instanceof AnotherExceptionType){
//handle this one
} else {
// We didn't expect this one. What could it be? Let's log it, and let it bubble up the hierarchy.
throw e;
}
}
However, this doesn't take into consideration the fact that IO can also throw Errors. Errors are not Exceptions. Errors are a under a different inheritance hierarchy than Exceptions, though both share the base class Throwable. Since IO can throw Errors, you may want to go so far as to catch Throwable
然而,这并没有考虑到 IO 也可能抛出错误的事实。错误不是例外。错误与异常属于不同的继承层次结构,尽管两者共享基类 Throwable。由于 IO 可以抛出错误,因此您可能想要捕获 Throwable
try{
// IO code
} catch (Throwable t){
if(t instanceof Exception){
if(t instanceof IOException){
// handle this exception type
} else if (t instanceof AnotherExceptionType){
//handle this one
} else {
// We didn't expect this Exception. What could it be? Let's log it, and let it bubble up the hierarchy.
}
} else if (t instanceof Error){
if(t instanceof IOError){
// handle this Error
} else if (t instanceof AnotherError){
//handle different Error
} else {
// We didn't expect this Error. What could it be? Let's log it, and let it bubble up the hierarchy.
}
} else {
// This should never be reached, unless you have subclassed Throwable for your own purposes.
throw t;
}
}
回答by Rathishkumar Nair
You may catch multiple exceptions in single catch block.
您可以在单个 catch 块中捕获多个异常。
try{
// somecode throwing multiple exceptions;
} catch (Exception1 | Exception2 | Exception3 exception){
// handle exception.
}