简体   繁体   English

异常处理-捕获多个异常无法正常工作

[英]Exception Handling - Catch with multiple exceptions not working as expected

I have a catch block with multiple exceptions - Arithmetic and NullPointer and one catch block with Exception. 我有一个具有多个异常的catch块-算术和NullPointer和一个具有Exception的catch块。 I am calling a method from catch block, but it is not find the instance of exception correctly. 我正在从catch块调用一个方法,但是找不到正确的异常实例。

try {
        int a = 10/0;
    } catch (ArithmeticException | NullPointerException e) {
        Exce(e);
    } catch (Exception e) {
        Exce(e);
    }

public static void Exce(ArithmeticException ex) {
    System.out.println("Arithmetic");
}

public static void Exce(Exception ex) {
    System.out.println("Exception");
}

But i am getting output as "Exception"..I am not sure why Arithmetic is not getting displayed 但是我将输出显示为“ Exception”。.我不确定为什么没有显示算术

When i have separate catch block for Arithmetic and Null pointer..I am able to print "Arithmetic"..But catch with multiple exceptions not working... 当我为算术和Null指针使用单独的catch块时..我能够打印“算术” ..但是有多个异常的catch无法正常工作...

If you split the ArithmeticException and NullPointerException into 2 catch blocks, it works as you expect. 如果将ArithmeticException和NullPointerException拆分为2个catch块,它将按预期工作。 I assume this is because variable e is declared of type Exception to be able to hold ArithmeticException and NullPointerException. 我认为这是因为变量e声明为Exception类型,以便能够容纳ArithmeticException和NullPointerException。

public static void main(String[] args) {
    try {
        int a = 10 / 0;
    } catch (ArithmeticException e) {
        Exce(e);
    } catch (NullPointerException e) {
        Exce(e);            
    } catch (Exception e) {
        Exce(e);
    }
}
} catch (ArithmeticException | NullPointerException e) {
    Exce(e);
}

The compile time type of e in the above is the union of ArithmeticException and NullPointerException . 上面e编译时间类型是ArithmeticExceptionNullPointerException并集。 So when the compiler tries to match the type of e against the Exce methods: 因此,当编译器尝试将e的类型与Exce方法进行匹配时:

  • The Exce(ArithmeticException) overload is not applicable because at runtime e could be a NullPointerException . Exce(ArithmeticException)重载不适用,因为在运行时e可能是NullPointerException

  • The Exce(Exception) overload is applicable because any value of e that matches the union of ArithmeticException and NullPointerException is an Exception as well. Exce(Exception)重载是适用的,因为任何与ArithmeticExceptionNullPointerException的并集匹配的e值也都是Exception

Unfortunately, you cannot declare a method overload for the ArithmeticException | NullPointerException 不幸的是,您不能为ArithmeticException | NullPointerException方法声明重载。 ArithmeticException | NullPointerException ... even if you wanted to: ArithmeticException | NullPointerException ...即使您想要:

  • If you want to handle ArithmeticException | NullPointerException 如果要处理ArithmeticException | NullPointerException ArithmeticException | NullPointerException together like this, you could declare an overload like this: 像这样一起ArithmeticException | NullPointerException ,您可以这样声明一个重载:

     public static void Exce(RuntimeException ex) { System.out.println("RuntimeException"); } 
  • Alternatively, catch ArithmeticException and NullPointerException separately. 或者,分别捕获ArithmeticExceptionNullPointerException

  • It is also possible to catch those two exception in one catch clause, and then discriminate them using (say) instanceof and type casts. 也可以在一个catch子句中捕获这两个异常,然后使用(例如) instanceof和类型强制转换来区分它们。 But it is a lot more code to do that. 但是要执行此操作需要更多代码。 (And a bad idea for other reasons.) (由于其他原因,这是一个坏主意。)

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

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