printStackTrace()
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.
The first line of output shows the same string which was returned by the toString() method for this object means Exception class name and later lines represent data previously recorded by the method fillInStackTrace().
Syntax:
public void printStackTrace()
Return Value: This method do not returns anything.
Below programs illustrate the printStackTrace method of Java.lang.Throwable class:
Example 1:
Java
// Java program to demonstrate // the printStackTrace () Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { testException1(); } catch (Throwable e) { // print stack trace e.printStackTrace(); } } // method which throws Exception public static void testException1() throws Exception { // create a ArrayIndexOutOfBoundsException Exception ArrayIndexOutOfBoundsException ae = new ArrayIndexOutOfBoundsException(); // create a new Exception Exception ioe = new Exception(); // initialize the cause and throw Exception ioe.initCause(ae); throw ioe; } } |
java.lang.Exception at GFG.testException1(File.java:36) at GFG.main(File.java:15) Caused by: java.lang.ArrayIndexOutOfBoundsException at GFG.testException1(File.java:32) ... 1 more
Example 2:
Java
// Java program to demonstrate // the printStackTrace () Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { testException1(); } catch (Throwable e) { // print stack trace e.printStackTrace(); } } // method which throws Exception public static void testException1() throws Exception { // create a ArrayIndexOutOfBoundsException Exception ArrayIndexOutOfBoundsException ae = new ArrayIndexOutOfBoundsException(); // create a new Exception Exception ioe = new Exception(); // initialize the cause and throw Exception ioe.initCause(ae); throw ioe; } } |
java.lang.Exception at GFG.testException1(File.java:36) at GFG.main(File.java:15) Caused by: java.lang.ArrayIndexOutOfBoundsException at GFG.testException1(File.java:32) ... 1 more
printStackTrace(PrintStream s)
The printStackTrace(PrintStream s) 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 to the specified print stream. This method works same as printStackTrace() but difference is only that it prints to specified print stream passed as parameter.
Syntax:
public void printStackTrace(PrintStream s)
Parameters: This method accepts PrintStream s as a parameter which is specified print stream where we want to write this Throwable details.
Return Value: This method do not returns anything.
Below programs illustrate the printStackTrace(PrintStream s) method of Java.lang.Throwable class:
Example 1:
Java
// Java program to demonstrate // the printStackTrace(PrintStream s) Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { // create a array of Integers int [] i = new int [ 2 ]; // try to add numbers to array i[ 2 ] = 3 ; } catch (Throwable e) { // print Stack Trace e.printStackTrace(System.out); } } } |
java.lang.ArrayIndexOutOfBoundsException: 2 at GFG.main(File.java:18)
Example 2:
Java
// Java program to demonstrate // the printStackTrace(PrintStream s) Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { testException1(); } catch (Throwable e) { // create printstream object PrintStream obj = new PrintStream(System.out); // print stack trace e.printStackTrace(obj); } } // method which throws Exception public static void testException1() throws Exception { throw new Exception( "System is Down" ); } } |
java.lang.Exception: System is Down at GFG.testException1(File.java:35) at GFG.main(File.java:15)
printStackTrace(PrintWriter s)
The printStackTrace(PrintWriter s) 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 to the specified print Writer. This method works same as printStackTrace() but difference is only that it prints to specified print Writer passed as parameter.
Syntax:
public void printStackTrace(PrintWriter s)
Parameters: This method accepts PrintWriter s as a parameter which is specified print writer where this Throwable details are to be written.
Return Value: This method do not returns anything.
Below programs illustrate the printStackTrace(PrintWriter s) method of Throwable class:
Example 1:
Java
// Java program to demonstrate // the printStackTrace(PrintWriter s) Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { // divide two numbers int a = 74 , b = 0 ; int c = a / b; } catch (Throwable e) { // Using a StringWriter, // to convert trace into a String: StringWriter sw = new StringWriter(); // create a PrintWriter PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); String error = sw.toString(); System.out.println( "Error:\n" + error); } } } |
Error: java.lang.ArithmeticException: / by zero at GFG.main(File.java:17)
Example 2:
Java
// Java program to demonstrate // the printStackTrace(PrintWriter s) Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { testException1(); } catch (Throwable e) { // Using a StringWriter, // to convert trace into a String: StringWriter sw = new StringWriter(); // create a PrintWriter PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); String error = sw.toString(); System.out.println( "Error:\n" + error); } } // method which throws Exception public static void testException1() throws Exception { throw new Exception( "Waiting for input but no response" ); } } |
Error: java.lang.Exception: Waiting for input but no response at GFG.testException1(File.java:38) at GFG.main(File.java:15)
References:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#printStackTrace()
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#printStackTrace(java.io.PrintStream)
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#printStackTrace(java.io.PrintWriter)