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.
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:
- An invalid array index is accessed, throwing ArrayIndexOutOfBoundsException.
- printStackTrace() prints the exception type, message, and call sequence.
- This provides a quick view of where the error occurred.
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:
- Exception type
- Exception message
- Method call sequence
- Class names
- Line numbers where the exception occurred
By default, the output is printed to the standard error stream.
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:
- An ArrayIndexOutOfBoundsException is explicitly created.
- This exception is set as the cause of another Exception using initCause().
- When printStackTrace() is called, Java prints the main exception first.
- The Caused by section follows, helping identify the root cause in chained exceptions.
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.
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:
- A custom PrintStream object is created and passed to printStackTrace().
- The exception message and stack trace are written to the specified stream instead of System.err.
- This method is useful for logging frameworks or redirecting error output to files or other destinations.
Syntax
public void printStackTrace(PrintStream s)
- Parameter: "s (PrintStream)" ->Destination where the stack trace will be printed.
- Return Value: This method do not returns anything.
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.
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:
- An ArithmeticException is thrown when a division by zero occurs.
- The stack trace is captured using a PrintWriter wrapped around a StringWriter.
- This converts the stack trace into a string, making it easy to log or include in error reports.
Syntax
public void printStackTrace(PrintWriter s)
- Parameter: "s (PrintWriter)"->Writer object used to store or output the stack trace.
- Return Value: This method do not returns anything.