Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

What is the simplest way to change the result of Throwable.getStackTrace() to a string that represents the stack trace?

1 Answer

0 votes
by (46k points)

the simplest way to change the result of Throwable.getStackTrace() to a string that depicts the stack trace is:

import java.io.StringWriter;

import java.io.PrintWriter;

// ...

StringWriter sw = new StringWriter();

PrintWriter pw = new PrintWriter(sw);

e.printStackTrace(pw);

String sStackTrace = sw.toString(); // stack trace as a string

System.out.println(sStackTrace);

This program records within the Throwable object data from the code above about the current state of the stack frames for the current thread.

If the stack trace of the Throwable is not writable, calling this process has no effect.

To read more about it click here.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in Java by Anvi (10.2k points)

Browse Categories

...