I am new to java.
I want something like debug_print_backtrace in java.
I want to print current stack trace to stdout or to intellij idea log window for debug.
I used to use debug_print_backtrace in php to find some stack infos in runtime for debug.
The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.
You can use Thread. currentThread(). getStackTrace() . That returns an array of StackTraceElement s that represent the current stack trace of a program.
To read this stack trace, start at the top with the Exception's type - ArithmeticException and message The denominator must not be zero . This gives an idea of what went wrong, but to discover what code caused the Exception, skip down the stack trace looking for something in the package com.
You only need one line.
new Exception().printStackTrace(System.out);
Thanks to Get current stack trace in Java
print stack trace to stderr:
new Exception().printStackTrace();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With