Page 1 of 1

About Question enthuware.ocajp.i.v7.2 . 827 :

Posted: Thu Jan 15, 2015 9:27 am
by gparLondon
When you use exception.printStackTrace(), a complete chain of the names of the methods called, along with the line numbers, is printed from the point where the exception was thrown and up to the point where the exception was caught and printStackTrace() was called.
I have this code:

Code: Select all

package com.oracle.certification.enthuware.exceptions;

public class PrintingStackTest {
	
	public static void main(String args[])
	{
		m1();
	}
        static void m1()
        {
           m2();
        }
       static void m2()
      {
         try
          {
	     m3();  
          }
          catch(Exception e)
          {
	    e.printStackTrace();
	  
          }
     }
     static void m3() throws Exception
    {
      throw new Exception();	
    }
}
The out put is:

Code: Select all

java.lang.Exception
	at com.oracle.certification.enthuware.exceptions.PrintingStackTest.m3(PrintingStackTest.java:27)
	at com.oracle.certification.enthuware.exceptions.PrintingStackTest.m2(PrintingStackTest.java:17)
	at com.oracle.certification.enthuware.exceptions.PrintingStackTest.m1(PrintingStackTest.java:11)
	at com.oracle.certification.enthuware.exceptions.PrintingStackTest.main(PrintingStackTest.java:7)
According to the explanation, I expected the out put to be

Code: Select all

java.lang.Exception
	at com.oracle.certification.enthuware.exceptions.PrintingStackTest.m3(PrintingStackTest.java:27)
	at com.oracle.certification.enthuware.exceptions.PrintingStackTest.m2(PrintingStackTest.java:17)
	at 
As the exception is caught and printStackTrace() was called in method m2.

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

Posted: Thu Jan 15, 2015 10:01 am
by admin
The explanation is incorrect. A throwable contains the details of the execution stack of the thread in which it is created. So that would basically mean that it will contain the names of the methods in the chain of method calls that led to the place where the exception was created going back up to the point where the thread, in which the exception was created, was started.

In your case, it was the main thread's stack trace that was printed.

thank you for informing us about it. This must be fixed asap.

HTH,
Paul.