Why is it so? This code compiles fine but throw an exception at run time
public class Except { public static void main(String args[]) { System.out.println("Hey, I am under main"); method(); } static void method() { throw new NullPointerException(); } } But this code isn't compiling
public class Expect { public static void main(String args[]) { System.out.println("Hey, I am under main"); method(); } static void method() { throw new IllegalAccessException(); } } Why this is so?That changing the type of exception,the second code is not compiling.Can somebody please explain,and I have used the throw statement in first code,and since its compiling fine,then what is the use of writing throws statement with method declaration. I am using jdk 1.8.0_45