Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the stack trace of an exception

Tags:

java

exception

How do I print the stack trace of an exception to a stream other than stderr? One way I found is to use getStackTrace() and print the entire list to the stream.

like image 248
r.v Avatar asked Jul 25 '11 22:07

r.v


People also ask

How do I print a stack trace?

Just use new Throwable(). printStackTrace() method and it will print complete stack trace from where a method is called, into the console.

How do I print exception details in python?

To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e . You can now print the error message with "print(e)" or use it for further processing.


1 Answers

Not beautiful, but a solution nonetheless:

StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter( writer ); exception.printStackTrace( printWriter ); printWriter.flush();  String stackTrace = writer.toString(); 
like image 109
Maurício Linhares Avatar answered Sep 18 '22 09:09

Maurício Linhares