Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (3.4k points)
How can I get the e.printStackTrace() and store it into a String variable? I want to use the string generated by e.printStackTrace() later in my program.

I'm still new to Java so I'm not too familiar with StringWriter that I think will be the solution. Or if you have any other ideas please let me know. Thanks

1 Answer

0 votes
by (46k points)

Something accompanying the lines of

StringWriter errors = new StringWriter();

ex.printStackTrace(new PrintWriter(errors));

return errors.toString();

Ought to be what you need.

Relevant documentation:

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 9, 2019 in Java by Nigam (4k points)
0 votes
1 answer
asked Jul 9, 2019 in Java by Aditya98 (1.3k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.4k questions

32.5k answers


108k users

Browse Categories