Convert Stack Trace to String in Java

Updated on April 23rd, 2018

Convert Stack Trace to String in Java

Handling exceptions in Java require an exception to storing in a file or might need it for some additional analysis than simply logging it. In this post, we will cover how to convert stack trace to string in Java.

1 Using Core Java API

Core Java API provides easy and efficient way to convert stack trace to string using StringWriter and PrintWriter

 

 
StringWriter writer = new StringWriter();
PrintWriter printWriter= new PrintWriter(writer);
exception.printStackTrace(printWriter);

 

 
public class StackTraceToStringJava {

    public static void main(String[] args) {

        try {
            throwDummyException();
        } catch (FileNotFoundException exception) {

            StringWriter writer = new StringWriter();
            PrintWriter printWriter= new PrintWriter(writer);
            exception.printStackTrace(printWriter);
            System.out.println("Exception in String is :: " + writer.toString());
        }
    }

    public static void throwDummyException() throws FileNotFoundException {
        throw new FileNotFoundException("Throwing file not found exception for demo purpose");
    }
}

 

Output

 
Exception in String is :: java.io.FileNotFoundException: Throwing file not found exception for demo purpose at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringJava.throwDummyException(StackTraceToStringJava.java:26)
at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringJava.main(StackTraceToStringJava.java:15)

Calling writer.toString() will provide stack trace in String format.

 

2 Using Apache Commons Lang API

Apache Commons Lang API ExceptionUtils class can be used to convert stack trace to String with a simple one line.

 


public class StackTraceToStringApache {

    public static void main(String[] args) {

        try {
            throwDummyException();
        } catch (FileNotFoundException exception) {
            String stackTraceAsString= ExceptionUtils.getStackTrace(exception);
            System.out.println("Strack Trace using Apache Commons :" + stackTraceAsString);
        }
    }

    public static void throwDummyException() throws FileNotFoundException {
        throw new FileNotFoundException("Throwing file not found exception for demo purpose Apache Commons Lang");
    }
}

Output


Strack Trace using Apache Commons :java.io.FileNotFoundException: Throwing file not found exception for demo purpose Apache Commons Lang
	at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringApache.throwDummyException(StackTraceToStringApache.java:23)
	at com.umeshawasthi.tutorials.corejava.exception.StackTraceToStringApache.main(StackTraceToStringApache.java:15)

To Apache Commons Lang API, we need to add it to classpath, We will use Maven for this (Simply add to lib folder if not using Maven)

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

3 Using Guava’s Throwable Class

Google Guava class provide Throwable utility class to convert stack trace to String

 String stackTrace = Throwables.getStackTraceAsString ( exception ) ;

We saw there are a number of different ways to Convert stack trace to String in Java using Java core API and third party API available.

Java 9 will be introducing StackWalker API which will provide more easy and efficient way to perform these operations.

 

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. Apache Commons Lang
  2. Guava Class Throwables
  3. Java 9 StackWalker

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of