![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
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)