Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print stacktrace for an exception Android [duplicate]

I want to print the stack trace because at the moment I have this running.

} catch (IOException e) {     throw new Error("Copying Failed"); } 

And I have been told to print e.stacktrace();

How do I do this?

like image 923
Somk Avatar asked Nov 21 '11 19:11

Somk


People also ask

How do I print StackTrace of exception?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How do you print StackTrace on string?

Example: Convert stack trace to a string In the catch block, we use StringWriter and PrintWriter to print any given output to a string. We then print the stack trace using printStackTrace() method of the exception and write it in the writer. Then, we simply convert it to string using toString() method.


2 Answers

} catch (IOException e) {     Log.e("YOUR_APP_LOG_TAG", "I got an error", e); } 

And check the LogCat for the output.

like image 169
Vit Khudenko Avatar answered Sep 22 '22 06:09

Vit Khudenko


An other method, very useful :

try { ... } catch (Exception e) {     Log.e(APP_TAG, "STACKTRACE");     Log.e(APP_TAG, Log.getStackTraceString(e)); } 
like image 33
Bourbon Avatar answered Sep 22 '22 06:09

Bourbon