Question
In Java, what is the simplest and most reliable way to convert the stack trace from an exception into a String representation?
For example, if I have a Throwable and want the full stack trace as text for logging, debugging, or storing somewhere, how can I turn it into a string that looks like the normal printed stack trace?
Throwable error = new RuntimeException("Something went wrong");
// How do I convert this stack trace to a String?
Short Answer
By the end of this page, you will understand how Java stack traces work, why Throwable.getStackTrace() does not directly give you the normal printed stack trace text, and how to correctly convert an exception stack trace into a String using StringWriter and PrintWriter. You will also see common mistakes, practical use cases, and a small mini-project.
Concept
A Java stack trace shows where an error happened and how the program reached that point. It usually includes:
- The exception type
- The error message
- The list of method calls leading to the error
- Sometimes a
Caused bychain for nested exceptions
A common beginner misunderstanding is assuming that Throwable.getStackTrace() returns a printable string. It does not. It returns an array of StackTraceElement objects:
StackTraceElement[] elements = throwable.getStackTrace();
That array contains structured stack-frame data, not the formatted text you normally see in logs.
If you want the familiar multi-line stack trace text, the usual approach is:
- Create a
StringWriter - Wrap it in a
PrintWriter - Call
throwable.printStackTrace(printWriter) - Read the text from the
StringWriter
This matters in real programming because stack traces are often:
- Logged to files
- Sent to monitoring systems
- Stored in error reports
- Returned in development tools
- Attached to API diagnostics in non-production environments
Using the correct approach ensures you preserve the full formatted trace, including nested causes.
Mental Model
Think of a stack trace like a breadcrumb trail showing how your program arrived at a failure.
getStackTrace()gives you the individual breadcrumbs as objects.printStackTrace()gives you the formatted story that humans usually read.
So if you want a readable string, do not just grab the breadcrumbs. Ask Java to write the story into a text buffer, then read that buffer as a string.
Syntax and Examples
The standard Java approach is:
import java.io.PrintWriter;
import java.io.StringWriter;
public class Main {
public static String stackTraceToString(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
return sw.toString();
}
}
Example usage
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (Exception e) {
String trace = stackTraceToString(e);
System.out.println(trace);
}
}
String {
();
(sw);
throwable.printStackTrace(pw);
sw.toString();
}
}
Step by Step Execution
Consider this example:
import java.io.PrintWriter;
import java.io.StringWriter;
public class Main {
public static void main(String[] args) {
try {
fail();
} catch (Exception e) {
String trace = stackTraceToString(e);
System.out.println(trace);
}
}
static void fail() {
throw new IllegalArgumentException("Bad input");
}
static String stackTraceToString(Throwable throwable) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
throwable.printStackTrace(pw);
return sw.toString();
}
}
What happens step by step
Real World Use Cases
Converting a stack trace to a string is useful in many practical situations:
Logging systems
You may want to store the full stack trace in a log message:
logger.error("Request failed: {}", stackTraceToString(e));
Error reporting
Applications often collect error details and send them to a monitoring service:
String errorDetails = stackTraceToString(e);
// send errorDetails to monitoring system
Saving debug information
You might write a stack trace into a file for troubleshooting:
Files.writeString(path, stackTraceToString(e));
Admin tools and diagnostics
A developer-only endpoint or support tool may show recent exception traces.
Background jobs
If a scheduled task fails, you may want to capture the stack trace as text and include it in an email or status report.
Real Codebase Usage
In real Java projects, developers usually do not convert stack traces to strings unless they have a specific reason. Many logging frameworks already accept the exception directly:
logger.error("Job failed", e);
That is often better because the logger can format the exception properly.
Still, converting to a string is common in these patterns:
Validation and diagnostics
When building structured error reports:
Map<String, String> errorReport = new HashMap<>();
errorReport.put("message", e.getMessage());
errorReport.put("stackTrace", stackTraceToString(e));
Guard clauses with detailed failure capture
if (config == null) {
throw new IllegalStateException("Configuration must not be null");
}
If this is caught later, the stack trace string may be attached to a support report.
Error handling pipelines
Some systems normalize exceptions into a common format:
- Error code
- User-safe message
- Internal debug message
- Stack trace string
API responses in development mode
In development, a server might include stack trace text in a debug response. In production, this is usually avoided for security reasons.
Common Mistakes
Mistake 1: Printing the array returned by getStackTrace()
Broken code:
System.out.println(e.getStackTrace());
Why it is wrong:
getStackTrace()returns an array- Printing the array directly does not format the stack trace
Better:
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String trace = sw.toString();
Mistake 2: Losing nested causes
Broken approach:
StringBuilder sb = new StringBuilder();
for (StackTraceElement element : e.getStackTrace()) {
sb.append(element).append("\n");
}
Why it is incomplete:
- It may omit the exception header
Comparisons
| Approach | What it returns | Human-readable | Includes full formatting | Best use |
|---|---|---|---|---|
throwable.getStackTrace() | StackTraceElement[] | Not directly | No | Programmatic inspection |
throwable.printStackTrace() | Prints to stream/writer | Yes | Yes | Console, files, writers |
throwable.printStackTrace(new PrintWriter(sw)) | String via writer buffer | Yes | Yes | Capture as text |
| Manual loop over stack frames | Custom text |
Cheat Sheet
import java.io.PrintWriter;
import java.io.StringWriter;
public static String stackTraceToString(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
}
Key rules
getStackTrace()returnsStackTraceElement[], not a formatted string.printStackTrace(PrintWriter)writes the normal stack trace format.StringWriterlets you capture printed text as a string.- Prefer
logger.error("message", e)when using a logging framework. - Avoid showing stack traces to end users in production.
Useful snippet
try {
// code
} catch (Exception e) {
String trace = stackTraceToString(e);
}
FAQ
Why doesn't getStackTrace() return a string?
Because Java separates the raw stack frame data from the formatted printed output. getStackTrace() gives structured data, not display text.
What is the standard way to convert a stack trace to a string in Java?
Use StringWriter and PrintWriter, then call throwable.printStackTrace(printWriter).
Is printStackTrace() the same as getStackTrace()?
No. getStackTrace() returns an array of stack frames. printStackTrace() produces the human-readable formatted trace.
Should I store stack traces in a database?
Sometimes, for diagnostics or error reporting. But avoid storing sensitive information unless necessary, and be careful with database size.
Can I manually join the StackTraceElement values?
You can, but it is often incomplete and may miss the exception message, nested causes, and suppressed exceptions.
Is it better to log the exception directly?
Usually yes. Most logging frameworks can format exceptions correctly when you pass the Throwable directly.
Can I use this for any , not just ?
Mini Project
Description
Build a small Java utility that safely captures exception details as text for debugging and reporting. This demonstrates the difference between exception objects, stack frame arrays, and readable stack trace strings. It also mirrors a common real-world helper method used in services, background jobs, and diagnostic tools.
Goal
Create a reusable method that converts any Throwable into a readable stack trace string and use it in a simple demo program.
Requirements
- Create a method that accepts a
Throwableand returns aString - Trigger an exception intentionally in a demo program
- Catch the exception and convert its stack trace to a string
- Print the resulting string to the console
- Keep the solution in plain Java using standard library classes only
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert an Array to a List in Java: Arrays.asList, Primitive Arrays, and Safe Alternatives
Learn how to convert arrays to lists in Java, including why Arrays.asList behaves differently for primitive arrays like int[].