Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print stack trace to stdout in java for debug?

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.

like image 381
bronze man Avatar asked Oct 09 '14 17:10

bronze man


People also ask

How do I print stack trace with system out?

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.

How can I get the current stack trace in Java?

You can use Thread. currentThread(). getStackTrace() . That returns an array of StackTraceElement s that represent the current stack trace of a program.

How do I Analyse Java stack trace?

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.


1 Answers

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();
like image 180
bronze man Avatar answered Oct 26 '22 17:10

bronze man