Throwable printStackTrace() method in Java with Examples (original) (raw)

Last Updated : 24 Jan, 2026

The printStackTrace() method in Java is widely used for debugging exceptions. It prints detailed information about an exception, helping developers understand where and why an error occurred during program execution.

Java `

class GFG{

public static void main(String[] args){
    
    try {
        int[] arr = new int[2];
        
        // This will throw ArrayIndexOutOfBoundsException
        arr[5] = 10; 
    } catch (Throwable e) {
        
        // Prints exception details
        e.printStackTrace(); 
    }
}

}

`

**Output:

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 2
at GFG.main(GFG.java:4)

**Explanation:

Syntax

public void printStackTrace()

Constructor of printStackTrace()

The printStackTrace() method provides several overloaded forms that allow developers to print exception stack trace details in different ways

1. printStackTrace() Method

The printStackTrace() method belongs to the java.lang.Throwable class. It prints the complete stack trace of an exception, including:

By default, the output is printed to the standard error stream.

Java `

class GFG { public static void main(String[] args) { try { testException1(); } catch (Throwable e) { e.printStackTrace(); } } static void testException1() throws Exception { ArrayIndexOutOfBoundsException ae = new ArrayIndexOutOfBoundsException(); Exception ex = new Exception(); ex.initCause(ae); throw ex; } }

`

**Output:

java.lang.Exception
at GFG.testException1(GFG.java:12)
at GFG.main(GFG.java:4)
Caused by: java.lang.ArrayIndexOutOfBoundsException
at GFG.testException1(GFG.java:10)

**Explanation:

Syntax

public void printStackTrace()

**Return Value: This method do not returns anything.

2. printStackTrace(PrintStream s) Method

This overloaded version prints the stack trace to a specified PrintStream instead of System.err. It provides flexibility to redirect exception output.

Java `

class GFG { public static void main(String[] args) { try { throw new Exception("System is Down"); } catch (Throwable e) { PrintStream ps = new PrintStream(System.out); e.printStackTrace(ps); } } }

`

Output

java.lang.Exception: System is Down at GFG.testException1(File.java:35) at GFG.main(File.java:15)

**Explanation:

Syntax

public void printStackTrace(PrintStream s)

3. printStackTrace(PrintWriter s) Method

This method prints the stack trace to a PrintWriter, often used with StringWriter for logging or storing errors as text. It is especially useful for web applications and log aggregation systems.

Java `

class GFG { public static void main(String[] args) { try { int a = 10 / 0; } catch (Throwable e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); System.out.println("Error:\n" + sw.toString()); } } }

`

Output

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

**Explanation:

Syntax

public void printStackTrace(PrintWriter s)