How to convert stacktrace to string in Java

Introduction :

Stack Trace helps to find out the root cause of an exception. Sometimes you may want to save the stack-trace in string format for further analysis, like saving it in your database, sending It to your server etc.

For example, you have one Android application and you want the application to send the stack-trace in string format to your own server if any exception occurs. Normally, we use a crashlytics service like “Fabric” to do it more easily, but what if you want to do it without using any third party ? In this tutorial, we will learn how to convert a stack-trace to a string in Java in two different ways. Let’s take a look :

Using PrintWriter and StringWriter :

This is a straight forward approach. The main idea is that function printStackTrace() can take PrintWriter as a parameter. Thus we can use one more StringWriter object and copy the PrintWriter content and use toString() to get the stack trace in string format.

StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);

For example :

import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    public static void main(String[] args) {
        try {
            generateDummyException();
        } catch (NullPointerException e) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            System.out.println("Exception generated " + stringWriter.toString());
        }
    }

    private static void generateDummyException() {
        throw new NullPointerException();
    }
}

It will print out the exception in string format. java exception to string

ExceptionUtils of Apache common langs library :

Apache commons lang library comes with a lot of useful utility classes. One of the mostly used class for dealing with exceptions is ExceptionUtils. This class has one method called getStackTarce that can be used to get the string representation of any Java exception.

We need to import Apache commons lang in our project to use this class. For a Maven project , pom.xml should include the following dependency :

    org.apache.commons
    commons-lang3
    3.8

For gradle projects, you can use the below dependency :

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8'

Make sure to check the latest repository version first.

After the import is completed, you can easily do the exception-string conversion like below :

String stacktrace = ExceptionUtils.getStackTrace(e);

Complete Program :

import org.apache.commons.lang3.exception.ExceptionUtils;

public class Main {
    public static void main(String[] args){
        try {
            generateDummyException();
        } catch (NullPointerException e) {
            String stacktrace = ExceptionUtils.getStackTrace(e);
            System.out.println("Exception generated " + stacktrace);
        }
    }
    private static void generateDummyException() {
        throw new NullPointerException();
    }
}

It will print the output same as like above. java exception to string conversion

Conclusion :

As you can see that stacktrace to string conversion in Java is really easy to implement. You can either use the core Java classes or Apache-commons-Lang library. Try to implement both examples we have shown above and if you have any queries, drop one comment below.

Similar tutorials :