简体   繁体   中英

Method having parameter “exception handling class objects” in java

Dear friends I am beginner to exception handling in java. I got this sample from one tutorial and I run this code its printed Arithmetic exception. If I remove that method which is have parameter as ArithmeticException then the first method called that is 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 .
2) If you remove method with Exception it chooses Object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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