-6
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.

Guru
  • 2,739
  • 1
  • 25
  • 27

1 Answers1

1

public interface B extends A is for adding more methods (or behaviors) to your java class.

public interface B implements A is not a valid code in java because you cannot create an interface by implementing another interface. When implementing an interface you need to write relevant code inside all the declared methods. Once you write code inside a method, you cannot name that as an interface anymore because interfaces just contains method signatures.