Page 1 of 1

About Question enthuware.ocajp.i.v7.2.834 :

Posted: Sun Jan 04, 2015 3:25 pm
by coder007
Calling method has to declare in its trows clause the same exception as in the source method, or it could be a super class of that exception?

Re: About Question enthuware.ocajp.i.v7.2.834 :

Posted: Sun Jan 04, 2015 9:46 pm
by admin
It could be a super class also. You should try it out.

Re: About Question enthuware.ocajp.i.v7.2.834 :

Posted: Thu Jan 08, 2015 11:27 pm
by coder007
Thank you, I will try it ))

Re: About Question enthuware.ocajp.i.v7.2.834 :

Posted: Mon Jul 06, 2020 3:27 pm
by Dreamweaver
The question is: What changes can be done to make the above code compile?
And the correct answer: Change declaration of all the three method to include throws Exception.

But for me it does not compile:
Exception in thread "main" java.lang.Exception
at com.teo.exam.tt.TestClass.yourMethod(TestClass.java:14)
at com.teo.exam.tt.TestClass.myMethod(TestClass.java:10)
at com.teo.exam.tt.TestClass.main(TestClass.java:6)

Code: Select all

public class TestClass {
    public static void main(String[] args) throws Exception {
        TestClass tc = new TestClass();
        tc.myMethod();
    }

    public void myMethod() throws Exception {
        yourMethod();
    }

    public void yourMethod() throws Exception {
        throw new Exception();
    }
}

Re: About Question enthuware.ocajp.i.v7.2.834 :

Posted: Mon Jul 06, 2020 4:55 pm
by admin
What you have posted is the exception stack trace generated by the JVM. That means compilation succeeded. Right?

Re: About Question enthuware.ocajp.i.v7.2.834 :

Posted: Tue Jul 07, 2020 5:34 am
by Dreamweaver
Thank you, I overlooked compile
Now is clear