Android - print full exception backtrace to log

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

This helper function also works nice since Exception is also a Throwable.

    try{
		//bugtastic code here
	}
	catch (Exception e)
	{
    	 Log.e(TAG, "Exception: "+Log.getStackTraceString(e));
	}

Solution 3 - Android

catch (Exception e) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  PrintStream stream = new PrintStream( baos );
  e.printStackTrace(stream);
  stream.flush();
  Log.e("MYAPP", new String( baos.toByteArray() );
}

Or... ya know... what EboMike said.

Solution 4 - Android

public String getStackTrace(Exception e){
  StringWriter sw = new StringWriter();
  PrintWriter pw = new PrintWriter(sw);
  e.printStackTrace(pw);
  return sw.toString();
}

Solution 5 - Android

e.printStackTrace() prints it to me. I don't think you're running the logcat correctly. Don't run it in a shell, just run

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

Solution 6 - Android

The standard output and error output are directed to /dev/null by default so it is all lost. If you want to log this output then you need to follow the instructions "Viewing stdout and stderr" shown here

Solution 7 - Android

try{
            ...
} catch (Exception e) {
     Log.e(e.getClass().getName(), e.getMessage(), e.getCause());
}

Solution 8 - Android

In the context of Android, I had to cast the Exception to a String:

try {
    url = new URL(REGISTRATION_PATH);
    urlConnection = (HttpURLConnection) url.openConnection();
} catch(MalformedURLException e) {
    Log.i("MALFORMED URL", String.valueOf(e));
} catch(IOException e) {
    Log.i("IOException", String.valueOf(e));
}

Solution 9 - Android

KOTLIN SOLUTION:

You can make use of the helper function getStackTraceString() belonging to the android.util.Log class to print the entire error message on console.

Example:

try { 
 
   // your code here
    
} catch (e: Exception) {

     Log.e("TAG", "Exception occurred, stack trace: " + e.getStackTraceString());

}

Solution 10 - Android

Kotlin extension. Returns the detailed description of this throwable with its stack trace.

e.stackTraceToString()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanView Question on Stackoverflow
Solution 1 - AndroidEboMikeView Answer on Stackoverflow
Solution 2 - AndroidGeorgeView Answer on Stackoverflow
Solution 3 - AndroidMark StorerView Answer on Stackoverflow
Solution 4 - AndroidVarun NarisettyView Answer on Stackoverflow
Solution 5 - AndroidFalmarriView Answer on Stackoverflow
Solution 6 - Androidtoc777View Answer on Stackoverflow
Solution 7 - AndroidFrank HouView Answer on Stackoverflow
Solution 8 - AndroidDaniel ViglioneView Answer on Stackoverflow
Solution 9 - AndroidArshakView Answer on Stackoverflow
Solution 10 - AndroidDanView Answer on Stackoverflow