Throwable printStackTrace() method in Java with Examples – GeeksforGeeks

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




 

import java.io.*;

 

class GFG {

 

    

    public static void main(String[] args)

        throws Exception

    {

 

        try {

 

            testException1();

        }

 

        catch (Throwable e) {

 

            

            e.printStackTrace();

        }

    }

 

    

    public static void testException1()

        throws Exception

    {

 

        

        ArrayIndexOutOfBoundsException

            ae

            = new ArrayIndexOutOfBoundsException();

 

        

        Exception ioe = new Exception();

 

        

        ioe.initCause(ae);

        throw ioe;

    }

}



Output: 

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




 

import java.io.*;

 

class GFG {

 

    

    public static void main(String[] args)

        throws Exception

    {

 

        try {

 

            testException1();

        }

 

        catch (Throwable e) {

 

            

            e.printStackTrace();

        }

    }

 

    

    public static void testException1()

        throws Exception

    {

 

        

        ArrayIndexOutOfBoundsException

            ae

            = new ArrayIndexOutOfBoundsException();

 

        

        Exception ioe = new Exception();

 

        

        ioe.initCause(ae);

        throw ioe;

    }

}



Output: 

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




 

import java.io.*;

 

class GFG {

 

    

    public static void main(String[] args)

        throws Exception

    {

        try {

 

            

            int[] i = new int[2];

 

            

            i[2] = 3;

        }

        catch (Throwable e) {

 

            

            e.printStackTrace(System.out);

        }

    }

}



Output: 

java.lang.ArrayIndexOutOfBoundsException: 2
    at GFG.main(File.java:18)

 

Example 2:
 

Java




 

import java.io.*;

 

class GFG {

 

    

    public static void main(String[] args)

        throws Exception

    {

 

        try {

 

            testException1();

        }

 

        catch (Throwable e) {

 

            

            PrintStream

                obj

                = new PrintStream(System.out);

 

            

            e.printStackTrace(obj);

        }

    }

 

    

    public static void testException1()

        throws Exception

    {

 

        throw new Exception("System is Down");

    }

}



Output: 

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




 

import java.io.*;

 

class GFG {

 

    

    public static void main(String[] args)

        throws Exception

    {

        try {

 

            

            int a = 74, b = 0;

 

            int c = a / b;

        }

        catch (Throwable e) {

 

            

            

            StringWriter sw = new StringWriter();

 

            

            PrintWriter pw = new PrintWriter(sw);

            e.printStackTrace(pw);

 

            String error = sw.toString();

 

            System.out.println("Error:\n" + error);

        }

    }

}



Output: 

Error:
java.lang.ArithmeticException: / by zero
    at GFG.main(File.java:17)

 

Example 2: 
 

Java




 

import java.io.*;

 

class GFG {

 

    

    public static void main(String[] args)

        throws Exception

    {

 

        try {

 

            testException1();

        }

 

        catch (Throwable e) {

 

            

            

            StringWriter sw = new StringWriter();

 

            

            PrintWriter pw = new PrintWriter(sw);

            e.printStackTrace(pw);

 

            String error = sw.toString();

 

            System.out.println("Error:\n" + error);

        }

    }

 

    

    public static void testException1()

        throws Exception

    {

        throw new Exception(

            "Waiting for input but no response");

    }

}



Output: 

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)
 

My Personal Notes

arrow_drop_up