I'm practicing some examples in Java. The code looks like this:
public class NullPassing {
public static void main(String[] args) {
method1(null);
}
public static void method1(Exception e) {
System.out.println(e);
}
public static void method1(ArithmeticException e) {
System.out.println(e);
}
}
I have few questions regarding this,
1) The above code executes and prints null by using method1(ArithmeticException e), Why isn't it using Exception(Since its tops the hierarchy) ?
Guess: Is it because of more specific variable ArithmeticException
specific compared to Exception
. If that's the case, why Question-2
2) By introducing the following method to the existing code, program shows compile time exception. Why?
public static void method1(NullPointerException e) {
System.out.println(e);
}
Help me learn. Thanks