简体   繁体   English

捕获多个异常的处理程序?

[英]Catch handler for multiple exceptions?

I am experimenting with exceptions and i want to ask when it is possible to handle multiple exceptions in one handler and when it is not? 我正在试验异常,我想问一问何时可以在一个处理程序中处理多个异常,什么时候不可以?

For example i wrote the following code which combines two exceptions ( FileNotFoundException OutOfMemoryError ) and the program runs properly without any error. 例如,我编写了以下代码,该代码结合了两个异常( FileNotFoundException OutOfMemoryError ),程序正常运行,没有任何错误。 Al thought the handling is not so relevant with the functionality of the code i chose them just to see when i can combine multiple exceptions in on handler : Al认为处理方式与我选择它们的代码功能不太相关,只是为了查看何时可以在处理程序中组合多个异常:

import java.io.FileNotFoundException;
import java.lang.OutOfMemoryError;

public class exceptionTest {
    public static void main(String[] args) throws Exception {
        int help = 5;

        try {
            foo(help);
        } catch (FileNotFoundException | OutOfMemoryError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static boolean foo(int var) throws Exception {
        if (var > 6)
            throw new Exception("You variable bigger than 6");
        else
            return true;
    }
}

But when i choose different type of exceptions the compiler gives me error . 但是当我选择不同类型的异常时,编译器给我错误。 For example when i choose IOException and Exception i have the error the exception is already handled " : 例如,当我选择IOException和Exception时,出现错误,则该异常已得到处理“:

import java.io.IOException;
import java.lang.Exception;



public class exceptionTest {
    public static void main(String[] args) throws Exception {
        int help = 5;

        try {
            foo(help);
        } catch (IOException | Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static boolean foo(int var) throws Exception {
        if (var > 6)
            throw new Exception("You variable bigger than 6");
        else
            return true;
    }

}

So why is this happening ? 那么为什么会这样呢? Why in one occasion i can use multiple exception in handler and in the other not ? 为什么在一种情况下我可以在处理程序中使用多个异常而在另一种情况下却不能呢? Thank you in advance. 先感谢您。

You are getting the message because IOException is a subclass of Exception . 您收到消息是因为IOExceptionException的子类。 Therefore, if an IOException were thrown, it would be caught by a catch (Exception e) statement, so catching it as an IOException is redundant. 因此,如果抛出IOException ,它将被catch (Exception e)语句捕获,因此将其作为IOException捕获是多余的。
The first example works because neither FileNotFoundException nor OutOfMemoryError is a subclass the other. 第一个示例有效,因为FileNotFoundExceptionOutOfMemoryError都不是另一个子类。

However, you can catch sub-classed exceptions using the separate catch statement: 但是,您可以使用单独的catch语句来捕获子类别的异常:

try{
    // code that might throw IOException or another Exception
} catch (IOException e) {
    // code here will execute if an IOException is thrown
} catch (Exception e) {
    // code here will execute with an Exception that is not an IOException
}

If you do this, please note that the subclass must come first. 如果这样做,请注意子类必须排在最前面。

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

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