How to print stacktrace for an exception Android
} catch (IOException e) {
Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}
And check the LogCat for the output.
Throw an exception and print its stacktrace in the logcat at error level
Try adding to your else statement
Log.e(TAG, "mymessage",e);
Whole code:
@Throws(RuntimeException::class)
fun throwErrorIfNotHttp (e: Throwable) {
if (e is HttpException) {
val code = e.code()
} else if (e is SocketTimeoutException) {
Log.e(TAG, "time out")
} else if (e is IOException) {
Log.e(TAG, "file error")
} else {
Log.e(TAG, "mymessage",e);
throw RuntimeException(e) // This should appear as error
}
}
companion object{
val TAG = "class_tag"
}
Is it a bad idea to use printStackTrace() for caugt Exceptions?
Yes, it is a bad idea. You should instead use Android's built-in log class specifically designed for these purposes: http://developer.android.com/reference/android/util/Log.html
It gives you options to log debug messages, warnings, errors etc.
Logging errors with:
Log.e(TAG, "message", e)
where the message can be an explanation of what was being attempted when the exception was thrown
or simply Log.e(TAG, e)
if you do not wish to provide any message for context
You can then click on the log console at the bottom while running your code and easily search it using the TAG or log message type as a filter
e.printStackTrace(); in string
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.
Android - print full exception backtrace to log
try {
// code that might throw an exception
} catch (Exception e) {
Log.e("MYAPP", "exception", e);
}
More Explicitly with Further Info
(Since this is the oldest question about this.)
The three-argument Android log methods will print the stack trace for an Exception
that is provided as the third parameter. For example
Log.d(String tag, String msg, Throwable tr)
where tr
is the Exception.
According to this comment those Log methods "use the getStackTraceString()
method ... behind the scenes" to do that.
Java / Android - How to print out a full stack trace?
There's overrides of all the log methods with (String tag, String msg, Throwable tr)
signatures.
Passing an exception as the third parameter should give you the full stacktrace in logcat.
Android Log.X not printing stacktrace
Turns out Android's Log.getStackTraceString which is used by Log.X swallows UnknownHostException. :(
From: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/util/Log.java#Log.getStackTraceString%28java.lang.Throwable%29
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return "";
}
// This is to reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
return "";
}
t = t.getCause();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
return sw.toString();
}
It's all very well reducing log spew but to not even tell me what my exception was is bad joojoo!
Related Topics
Android: How to Change the Actionbar "Home" Icon to Be Something Other Than the App Icon
Generate Barcode Image in Android Application
Save Sensitive Data in React Native
How to Change the Android Startactivity() Transition Animation
How to Declare Git Repository as Dependency in Android Gradle
Change the Right Margin of a View Programmatically
Android Retrofit - Onprogressupdate for Showing Progress Notification
Cannot Lower Case Button Text in Android Studio
Filter Output in Logcat by Tagname
How to Combine Multiple Images into a Single Image in Android
Android: Using Activitygroup to Embed Activities
How to Share Photo with Caption via Android Share Intent on Facebook
Cache Images Local, from Google Firebase Storage
Travis Ci Failed Because Cannot Accept License Constrain Layout
How to Capture an Image in Background Without Using the Camera Application