Java Throwable printStackTrace() Method



Description

The Java Throwable printStackTrace(PrintWriter s) method prints this throwable and its backtrace to the specified print writer.

Declaration

Following is the declaration for java.lang.Throwable.printStackTrace() method

public void printStackTrace(PrintWriter s)

Parameters

s − This is the PrintWriter to use for output.

Return Value

This method does not return any value.

Exception

NA

Example: Printing Stacktrace of IllegalArgumentException

The following example shows the usage of Java Throwable printStackTrace() method. We've defined a method getStackTrace() which creates a PrintWriter object, and using printStackTrace() method, the stacktrace is written to the printWriter object and finally using toString(), this stacktrace is returned. In main method, getStackTrace() method is called with an IllegalArgumentException object and result is printed.

package com.tutorialspoint;

import java.io.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      Throwable t = new IllegalArgumentException("ABCD");
      System.out.println(getStackTrace(t));  
   }
 
   public static String getStackTrace(Throwable t) {
  
      /* prints this throwable and its backtrace to the specified
      print writer. */
      Writer wr = new StringWriter();
      PrintWriter pWriter = new PrintWriter(wr);
      t.printStackTrace(pWriter);
      return wr.toString();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.lang.IllegalArgumentException: ABCD
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:9)

Example: Printing Stacktrace of Throwable

The following example shows the usage of Java Throwable printStackTrace() method. We've defined a method getStackTrace() which creates a PrintWriter object, and using printStackTrace() method, the stacktrace is written to the printWriter object and finally using toString(), this stacktrace is returned. In main method, getStackTrace() method is called with an Throwable object and result is printed.

package com.tutorialspoint;

import java.io.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      Throwable t = new Throwable("ABCD");
      System.out.println(getStackTrace(t));  
   }
 
   public static String getStackTrace(Throwable t) {
  
      /* prints this throwable and its backtrace to the specified
      print writer. */
      Writer wr = new StringWriter();
      PrintWriter pWriter = new PrintWriter(wr);
      t.printStackTrace(pWriter);
      return wr.toString();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.lang.Throwable: ABCD
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:9)

Example: Printing Stacktrace of Exception

The following example shows the usage of Java Throwable printStackTrace() method. We've defined a method getStackTrace() which creates a PrintWriter object, and using printStackTrace() method, the stacktrace is written to the printWriter object and finally using toString(), this stacktrace is returned. In main method, getStackTrace() method is called with an Exception object and result is printed.

package com.tutorialspoint;

import java.io.*;

public class ThrowableDemo {

   public static void main(String[] args) {

      Throwable t = new Exception("ABCD");
      System.out.println(getStackTrace(t));  
   }
 
   public static String getStackTrace(Throwable t) {
  
      /* prints this throwable and its backtrace to the specified
      print writer. */
      Writer wr = new StringWriter();
      PrintWriter pWriter = new PrintWriter(wr);
      t.printStackTrace(pWriter);
      return wr.toString();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.lang.Exception: ABCD
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:9)
java_lang_throwable.htm
Advertisements