In this tutorial, we illustrate how to use printStackTrace() method. printStackTrace() method is the class of Throwable class and is used to print the stack trace to the console and line numbers of statement that called the methods.With the use of printStackTrace() method, java provides a way to get the error message in detail.Compare to getMessage() method that is used with an object of the Exception class to print errors to debug the process, printStackTrace() is more effective and informative for debugging process.

Java Code: Program to print stack trace
public class PrintStack {

    public static void main(String args[]) {
        String str = "Exception";
        int len = 0;
        try {
            StringBuffer sbuf = new StringBuffer(str);
            len = str.length();
            for (int ct = len; ct >= 0; ct--) {
                System.out.print(sbuf.charAt(ct));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Output of the program:

How to print stack trace message in java programs?-massage.jpg
Figure: Output showing stack trace of exception