Determining Current Call Stack (For Diagnostic Purposes)
Solution 1:
I think you can get the same thing with:
StackTraceElement[] cause = Thread.currentThread().getStackTrace();
Solution 2:
Well, you can improve it slightly by not actually throwing the exception.
Exception ex = new Exception();
ex.fillInStackTrace();
StackTraceElement[] cause = ex.getStackTrace();
Actually, I just checked: the constructor calls fillInStackTrace()
already. So you can simplify it to:
StackTraceElement[] cause = new Exception().getStackTrace();
This is actually what Thread.getStackTrace()
does if it's called on the current thread, so you might prefer using it instead.
Solution 3:
If you want it as a String and use Apache Commons:
org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(new Throwable())