Exception to String

Given an exception, how do I convert the stack trace into a string? I want to capture the entire contents, the message and the trace, to a string.

The answer to this question at first glance seems trivial, but is actually something that can stump someone who’s advanced beyond the rank of beginner. Java programmers usually print stack traces printStackTrace() and fetch exception messages as strings with getMessage(). Hardly ever does anyone want to capture a stack trace as a string. That appears to be why JavaSoft did not provide a getStackTrace() convenience method.

The java.lang.Throwable class provides three printStackTrace methods. The most commonly used is the one that takes no arguments, which prints a stack trace to standard output by default. There is no need to separately print the message accompanying an exception when you print a stack trace, becausethe stack trace includes the message. The two other printStackTrace methods take PrintStream and PrintWriter arguments. This means that if you want to store a stack trace in a string, you have to write to it through either a PrintStream or a PrintWriter.

The simplest way to do this is to wrap a StringWriter with a PrintWriter. The accompanying code listing shows how to do this. Since this is an operation you might want to perform often, thenecessary code has been encapsulated in a static method called getStackTrace, which does the necessary dirty work.

import java.io.*;public final class StringStackTrace {  public static final String getStackTrace(Throwable e) {    PrintWriter printer;    StringWriter string;    string  = new StringWriter();    printer = new PrintWriter(string);    e.printStackTrace(printer);    printer.close();    return string.toString();  }  public static final void main(String args[]) {    String trace = null;    try {      throw new Exception("This is a test.");    } catch(Exception e) {      trace = StringStackTrace.getStackTrace(e);    }    System.out.println(trace);  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.