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

5
  • 3
    Your option 2. doesn't compile Commented Oct 3, 2016 at 8:29
  • Similar to : stackoverflow.com/q/9494380/5395773 Commented Oct 3, 2016 at 8:32
  • Classes implement interfaces. Interfaces extends other interfaces. Commented Oct 3, 2016 at 8:33
  • Not a good duplicate... doesn't say anything about interfaces extending other interfaces. Commented Oct 3, 2016 at 8:48
  • Now there are two options in Java---no, there aren't. That's the only answer you need. Commented Oct 3, 2016 at 8:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.