Home Tutorial Java Core Java Thread getStackTrace Example

 
 

Share on Google+Share on Google+
Java Thread getStackTrace Example
Posted on: October 26, 2012 at 12:00 AM
Advertisement
This section explains use of getStackTrace() method in java Thread.

Java Thread getStackTrace Example

This section explains use of getStackTrace() method  in java Thread.

Thread getStackTrace() :

It returns an array of StackTraceElement, and individual of them shows one stack frame. acts as the stack dump of the given thread. This method provides the array of stack trace elements which can access to the stack trace information. Stack dump of thethread is represented by the stack trace elements. If thread is not presented then this method will return no element in array that is zero length array. If length is non zero then the element at zero position represents
the top of the stack,and this represent last method invocation of the sequence. In this point you can create throwable and thrown. The last array element shows the stack bottom and it is the first method invocation in the sequence.
It throws SecurityException exception.

Example :  In this example we are throwing exception by using getStackTrace() method.

public class GetStackTrace {
	public static void main(String args[]) {
		try {
			NewException();
		} catch (Throwable e) {
			/* Gets the stack trace. */
			StackTraceElement[] trace = e.getStackTrace();
			System.err.println("" + trace[0].toString());
		}
	}

	public static void NewException() throws Throwable {
		Throwable throwable = new Throwable("New Exception...");
		StackTraceElement[] element = new StackTraceElement[] { new StackTraceElement(
				"GetStackTrace", "NewException", "Roseondia", 10) };
		/* Sets the stack trace elements that will be returned */
		
		throwable.setStackTrace(element);
		throw throwable;
	}
}

Output :

GetStackTrace.NewException(Roseondia:10)
Advertisement

Related Tags for Java Thread getStackTrace Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 26, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com