简体   繁体   English

在一个catch块中处理所有Java异常

[英]Handling all Java Exceptions in one catch block

When I try to write this code : 当我尝试编写此代码时:

catch (Exception  | OutOfMemoryError| NumberFormatException| SQLException| IOException  e){
}

I get the error: "The exception NumberFormatException is already caught by the alternative Exception" I understand it great and from first thinking it's make sense because Exception is the general one. 我收到错误消息:“异常NumberFormatException已被替代的Exception捕获”,我理解得很好,从一开始就认为这是有道理的,因为Exception是通用的。 but when I write this code: 但是当我编写这段代码时:

 catch (Exception  | OutOfMemoryError  e){
    }

It works without any problems so now I start to get confused why don't I get the previous error. 它可以正常工作,所以现在我开始感到困惑,为什么我没有收到先前的错误。 Is OutOfMemmoryError will catch things that Exception won't catch? 是OutOfMemmoryError会捕获Exception不会捕获的内容吗? If I Want to make catch that will include all of exceptions and errors possible what should i write? 如果我想抓住所有可能出现的异常和错误,该怎么写? I just understood that this: 我只是了解这一点:

catch (Exception e){
}

won't catch for me all the exceptions and errors so what will do it? 不会为我捕获所有异常和错误,那么该怎么办?

The code catch (Exception | OutOfMemoryError e){ } compiles because OutOfMemoryError is not an Exception , so Exception doesn't cover OutOfMemoryError . 代码catch (Exception | OutOfMemoryError e){ }编译,因为OutOfMemoryError不是Exception ,所以Exception不会覆盖OutOfMemoryError OutOfMemoryError is an Error and it is not an Exception . OutOfMemoryError是一个Error ,它不是Exception

It's usually not a good idea to catch Error s, because they are usually unrecoverable anyway. 捕获Error通常不是一个好主意,因为无论如何它们通常都无法恢复。

To better understand what's happening, take a look at the Java exception hierarchy. 为了更好地了解正在发生的事情,请看一下Java异常层次结构。 A broad overview can be seen in this diagram . 此图中可以看到广泛的概述。

When you catch multiple exceptions in a single catch block, you should only use the "topmost" exceptions in catch. 当在单个catch块中捕获多个异常时,应仅在catch中使用“最高”异常。 For example, your error will persist in something like 例如,您的错误将持续出现在类似

catch (IOException | FileNotFoundException e)

since FileNotFoundException extends IOException . 由于FileNotFoundException扩展了IOException

In your code, all exceptions (except OutOfMemoryError ) are subtypes of java.lang.Exception , hence the error. 在您的代码中,所有异常( OutOfMemoryError除外)都是java.lang.Exception子类型,因此是错误。 OutOfMemoryError is a subtype of java.lang.Error , which is a Throwable , but not an Error . OutOfMemoryErrorjava.lang.Error的子类型,它是Throwable而不是Error

Here is a more fine-grained diagram representing the hierarchy of all java.lang.Exception s within Throwable . 这是一个更细粒度的图,表示Throwable中所有java.lang.Exception的层次结构

Hope this helped. 希望这会有所帮助。

Finally, if you really want to catch all exceptions and errors in one catch block (a terrible idea, as others have pointed out), you could do one of the following: 最后,如果您真的想在一个catch块中捕获所有异常和错误(一个可怕的主意,正如其他人指出的那样),则可以执行以下操作之一:

catch (Exception | Error e)

or 要么

catch (Throwable t)

You can catch Throwable , which is a superclass of everything, that can be thrown, errors or exceptions. 您可以捕获Throwable ,它是所有可以抛出的所有超类,错误或异常。

As pointed out previously though, this is not a good idea, and not something you should do. 如前所述,这不是一个好主意,也不是您应该做的事情。 You should only be catching those exceptions from which you can meaningfully recover. 您应该只捕获可以从中有意义地恢复的那些异常。

不允许在multi-catch语句中指定相同层次结构的两个或多个异常。

如果您想捕获“所有内容”,则可以捕获(Throwable t)捕获错误,异常,运行时异常等。尽管我建议捕获单个异常,因为您可能希望应用程序处理不同类型的异常/错误。 /不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM