简体   繁体   English

Java中具有参数“异常处理类对象”的方法

[英]Method having parameter “exception handling class objects” in java

Dear friends I am beginner to exception handling in java. 亲爱的朋友,我是Java异常处理的初学者。 I got this sample from one tutorial and I run this code its printed Arithmetic exception. 我从一个教程中获得了此示例,并运行了该代码的打印的Arithmetic异常。 If I remove that method which is have parameter as ArithmeticException then the first method called that is print Exception. 如果我删除参数为ArithmeticException的方法,则第一个调用的方法是print Exception。 Please any one can explain what is happening with this code. 请任何人都可以解释这段代码的情况。

public class Question1 {

  public static void javaHungry(Exception e) {
    System.out.println("Exception");
  }

  public static void javaHungry(ArithmeticException ae) {
    System.out.println("ArithmeticException");
  }

  public static void javaHungry(Object obj) {
    System.out.println("Object");
  }

  /**
  * @param args
  */
  public static void main(String[] args) {
    javaHungry(null);
  }

}

When you overload methods and passed a parameter which suits for all, the most specific methods can be choose at run time. 当您重载方法并传递适合所有人的参数时,可以在运行时选择最特定的方法。

The order of specificiation here is 这里的指定顺序是

ArithmeticException  > Exception  > Object 

1) If you remove method with ArithmeticException it chooses Exception . 1)如果使用ArithmeticException删除方法,则选择Exception
2) If you remove method with Exception it chooses Object . 2)如果使用Exception删除方法,则选择Object

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

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