Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

Android - print full exception backtrace to log

I have a try/catch block that throws an exception and I would like to see information about the exception in the Android device log.

I read the log of the mobile device with this command from my development computer:

/home/dan/android-sdk-linux_x86/tools/adb shell logcat

I tried this first:

try {
    // code buggy code
} catch (Exception e)
{
    e.printStackTrace();
}

but that doesn't print anything to the log. That's a pity because it would have helped a lot.

The best I have achieved is:

try {
    // code buggy code
} catch (Exception e)
{
    Log.e("MYAPP", "exception: " + e.getMessage());             
    Log.e("MYAPP", "exception: " + e.toString());
}

Better than nothing but not very satisfying.

Do you know how to print the full backtrace to the log?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...