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.
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.
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.
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"
}
Where do I see printed stack trace in Android Studio?
Stack trace will be send to default (console) output. In Android Studio (which is based on IntelliJ idea) is that window called logcat.
@See image below.
If you can not see this tab, go to Window > and click Restore Default Layout
You can click Shift + F12 as well.
incomplete backtrace in logcat
The answer appears to be to upgrade to Android 4.3. My phone received an update today (the custom HTC version rolled out to developer edition devices) and the backtraces dumped to the system log now reveal a full stack. However, as @user1034749 suggests, getting to grips with GDB is a good alternative with many additional benefits.
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.
Related Topics
How to Draw a Circle with Animation in Android with Circle Size Based on a Value
Android - Apply Selectableitembackground in Xml with Support V7
Endless Recyclerview with Progressbar for Pagination
Adding Tab Inside Fragment in Android
How to Combine Bottomappbar + Fab with Bottomnavigationview
Android Google+ Integration - Repeated Userrecoverableauthexception
Android: Picasso Load Image Failed . How to Show Error Message
Bitmap Is Returning Null from Bitmapfactory.Decodefile(Filename)
Generate Barcode Image in Android Application
What Is the Default Font Family in Android
Android Application Class Lifecycle
How to Get Add to Home Screen Pop Up on Site Open in Mobile Browser
Dynamically Add Textviews to a Linearlayout