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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests