public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(
String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
please explain why I am getting this error
C:\JAVA>javac ThrowsDemo.java
ThrowsDemo.java:4: Exception java.lang.IllegalAccessException must be caught, or it must be declared in th
e throws clause of this method.
throw new IllegalAccessException("demo");
^
ThrowsDemo.java:9: Exception java.lang.IllegalAccessException is never thrown in the body of the correspon
ding try statement.
} catch (IllegalAccessException e) {
^
2 errors
Here IllegalAccessException is thrown and caught in Try block.
Please explain this
Thanks in advance