Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Something like Environment.StackTrace in .Net.
Environment.StackTrace
BTW, Thread.dumpStack() is not what I want - I want to get the stacktrace back, not print it out.
Thread.dumpStack()
You can use Thread.currentThread().getStackTrace()That returns an array of StackTraceElements that represent the current stack trace of a program.
Thread.currentThread().getStackTrace();is fine if you don't care what the first element of the stack is.new Throwable().getStackTrace();will have a defined position for your current method, if that matters.
Thread.currentThread().getStackTrace();is available since JDK1.5.For an older version, you can redirect exception.printStackTrace() to a StringWriter() :StringWriter sw = new StringWriter();new Throwable("").printStackTrace(new PrintWriter(sw));String stackTrace = sw.toString();
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { System.out.println(ste + "\n");}
To get the stack trace of all threads you can either use the jstack utility, JConsole or send a kill -quit signal (on a Posix operating system).However, if you want to do this programmatically you could try using ThreadMXBean:ThreadMXBean bean = ManagementFactory.getThreadMXBean();ThreadInfo[] infos = bean.dumpAllThreads(true, true);for (ThreadInfo info : infos) { StackTraceElement[] elems = info.getStackTrace(); // Print out elements, etc.}As mentioned, if you only want the stack trace of the current thread it's a lot easier - Just use Thread.currentThread().getStackTrace();
Silly me, it's Thread.currentThread().getStackTrace();
try {}catch(Exception e) { StackTraceElement[] traceElements = e.getStackTrace(); //...}or Thread.currentThread().getStackTrace()
You can use apache's commons for that:String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);
A far easier way is to use this:String stackTrace = Log.getStackTraceString(exception);
I have a utility method that returns a string with the stacktrace:static String getStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); t.printStackTrace(pw); pw.flush(); sw.flush(); return sw.toString();}And just logit like...... catch (FileNotFoundException e) { logger.config(getStackTrace(e));}
You can use Thread.currentThread().getStackTrace()
ReplyDeleteThat returns an array of StackTraceElements that represent the current stack trace of a program.
Thread.currentThread().getStackTrace();
ReplyDeleteis fine if you don't care what the first element of the stack is.
new Throwable().getStackTrace();
will have a defined position for your current method, if that matters.
Thread.currentThread().getStackTrace();
ReplyDeleteis available since JDK1.5.
For an older version, you can redirect exception.printStackTrace() to a StringWriter() :
StringWriter sw = new StringWriter();
new Throwable("").printStackTrace(new PrintWriter(sw));
String stackTrace = sw.toString();
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
ReplyDeleteSystem.out.println(ste + "\n");
}
To get the stack trace of all threads you can either use the jstack utility, JConsole or send a kill -quit signal (on a Posix operating system).
ReplyDeleteHowever, if you want to do this programmatically you could try using ThreadMXBean:
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] infos = bean.dumpAllThreads(true, true);
for (ThreadInfo info : infos) {
StackTraceElement[] elems = info.getStackTrace();
// Print out elements, etc.
}
As mentioned, if you only want the stack trace of the current thread it's a lot easier - Just use Thread.currentThread().getStackTrace();
Silly me, it's Thread.currentThread().getStackTrace();
ReplyDeletetry {
ReplyDelete}
catch(Exception e) {
StackTraceElement[] traceElements = e.getStackTrace();
//...
}
or
Thread.currentThread().getStackTrace()
You can use apache's commons for that:
ReplyDeleteString fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);
A far easier way is to use this:
ReplyDeleteString stackTrace = Log.getStackTraceString(exception);
I have a utility method that returns a string with the stacktrace:
ReplyDeletestatic String getStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
t.printStackTrace(pw);
pw.flush();
sw.flush();
return sw.toString();
}
And just logit like...
...
catch (FileNotFoundException e) {
logger.config(getStackTrace(e));
}