Converting a Stack Trace to a String in Java

1. Introduction

When dealing with exceptions in Java, we’re frequently logging or simply displaying stack traces. However, sometimes, we don’t want just to print the stack trace, we might need to write the stack trace to a file, to a database or even transmit it over the network.

For these purposes, having the stack trace as a String would be very useful. And unfortunately, Java doesn’t provide a very convenient method to do that directly.

2. Conversion with Core Java

Let’s start with the core library.

The function printStackTrace() of the Exception class can take one parameter, either a PrintStream or a PrintWriter. Thus, it is possible, using a StringWriter, to print the stack trace into a String:

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);

Then, calling sw.toString() will return the stack trace as a String.

3. Conversion with Commons-Lang

While the previous method is the simplest way of converting a stack trace to a String using core Java, it remains a bit cumbersome. Fortunately, Apache Commons-Lang provides a function doing the job.

Apache Commons-Lang is a very useful library offering a lot of features that are missing in the core classes of the Java API, including classes that can be used to work with the exceptions.

First, let’s start with the project configuration. When using Maven, we just have to add the following dependency to the pom.xml:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>

Then, in our case, the most interesting class is ExceptionUtils, which provides functions to manipulate the exceptions. Using this class, getting the stack trace as a String from an Exception is pretty straightforward:

String stacktrace = ExceptionUtils.getStackTrace(e);

4. Conclusion

Getting the stack trace of an exception as a String isn’t difficult, but it’s far from being intuitive. This article presents two ways of doing it, either using core Java or using Apache Commons-Lang.

Keep in mind that Java 9 will bring a new StackWalking API which should make things easier.

As always, the code samples can be found here on GitHub.

Related posts:

Spring Security 5 for Reactive Applications
Serialization và Deserialization trong java
Giới thiệu Google Guice – Dependency injection (DI) framework
Spring Boot - Enabling HTTPS
Spring Autowiring of Generic Types
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java program to Implement Tree Set
Giới thiệu Google Guice – Binding
Java Program to Implement LinkedTransferQueue API
Weak References in Java
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Segment Tree
Simplify the DAO with Spring and Java Generics
Java Program to Implement Johnson’s Algorithm
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
A Guide to JUnit 5 Extensions
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Implement Extended Euclid Algorithm
Java Switch Statement
Create a Custom Auto-Configuration with Spring Boot
Java Program to Implement Graph Coloring Algorithm
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Java Program to Implement Attribute API
Removing Elements from Java Collections
Java Program to Implement Sieve Of Atkin
Giới thiệu Google Guice – Injection, Scope
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Implement CopyOnWriteArrayList API
Java – InputStream to Reader
Comparing Strings in Java