There are e.printStackTrace()
method to print exceptional error, so I would like to take entire exception in String
and show it by Toast.makeText()
How can i do this?
If there are more alternate idea, then please share with me or suggest me.

- 20,270
- 5
- 40
- 73

- 21,853
- 23
- 89
- 133
-
What is your goal with the bounty? Do you need more information than given in the accepted answer, do you want alternative answers (in which direction?), do you simply want to give an additional reward for the existing answer? Some comment about this would be nice, so possible answerers now what you want in an answer. – Paŭlo Ebermann Sep 08 '11 at 23:04
-
i need more information about it – Nikunj Patel Sep 09 '11 at 04:22
5 Answers
Use the following piece of code:
Writer writer = new StringWriter();
exception.printStackTrace(new PrintWriter(writer));
String s = writer.toString();
There used to be a way to extract an exception stacktrace into the String in one line with Log.getStackTraceString
call. But starting from Android 4.0 (API 14) that method is not reliable anymore, as it returns an empty string for UnknownHostException
(see Android issue #21436 for the details, in short: "to reduce the amount of log spew that apps do in the non-error condition of the network being unavailable" Android engineers made IMHO a dubious decision to modify Log.getStackTraceString
method).
Thus it is better to use the code I provided at the beginning of this post.

- 28,673
- 12
- 97
- 125
-
actual i have develop one app in which so many swf are stay in app.and those swf are not play in emulator so each time i have test it in device. but device has limitation that not show error. so i have find out this solution – Nikunj Patel Aug 30 '11 at 12:09
-
1great solution because others dont always point to the exact line of error!! – Vicky Kapadia May 12 '12 at 05:25
-
`// This is to reduce the amount of log spew that apps do in the non-error // condition of the network being unavailable.` as to why `UnknownHostException` was removed – Dheeraj Bhaskar Jan 22 '15 at 11:08
import android.util.Log;
...
String stackTrace = Log.getStackTraceString(e);

- 17,524
- 4
- 45
- 76
It's doable, but don't do this. Show just the error message (even that is too much for 'real' users), the full stack trace should go to the log only.

- 52,576
- 10
- 84
- 84
-
2Error != full stack trace. A stack trace shown as a toast will be mostly unreadable. Plus it will disappear after a few seconds, and you won't have time to read it (since it's quite long). – Nikolay Elenkov Aug 30 '11 at 12:15
you can print the stack trace to a stream & read from it.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
e.printStackTrace(pw);
String stachTrace = new String(baos.toByteArray());
or you can use a StringWriter in place of the ByteArrayOutputStream.

- 9,920
- 4
- 33
- 35
-
So this is how I could write the stack trace to a text file or to the console so that I can look at if for later use. – Doug Hauf Feb 12 '14 at 14:24
-
you could also consider a logging framework (which would be a better idea) like log4j, slf4j etc., – Anantha Sharma Feb 12 '14 at 15:15
In your exception handler use:
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
whateverFunctionYouLikeToPrintYourStackTrace(sw.getBuffer().toString());
However, you're much better off using ADB with logcat, because stack traces on Toasts look terrible.

- 3,186
- 1
- 20
- 26
-
What are Toasts anyway? I just want to print my stacktrace to a text file if I can. – Doug Hauf Feb 12 '14 at 14:25
-
Toasts are the annoying gray box in the lower middle section of the screen that are system wide that some rather important software misues on Android (Twitter, Gmail et al). They are only good to debug, and then that even isn't good anymore. Use logcat, that's the preferred approach. – Chiguireitor Feb 12 '14 at 15:59