0 votes
1 view
in Java by (4.1k 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 (46.1k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...