public class ThrowDemo {
public static void catchAndThrowNullPointerException() {
try {
throw new NullPointerException(); // Line Number 7
}
catch(NullPointerException npe) {
System.out.println("NPE thrown");
try {
throw npe;
}
catch(Exception ex) {
System.out.println("Exception thrown inside nested try.");
throw ex; // Line 16
}
}
}
public static void main(String args[]) {
catchAndThrowNullPointerException();
}
}
When Run, provides following output:
NPE thrown
Exception thrown inside nested try.
Exception in thread "main" java.lang.NullPointerException
at exception.handling.ThrowDemo.catchAndThrowNullPointerException(ThrowDemo.java:7)
at exception.handling.ThrowDemo.main(ThrowDemo.java:22)
Is this an expected behavior, if so why is the actual line number 16 which is causing the exception to be thrown to the main function not being included in the stack trace? The reason for this doubt being, if Line 16 is commented, there is not stacktrace printed in the output.
Thanks in advance.