103

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.

Dan
  • 15,948
  • 20
  • 63
  • 92

11 Answers11

177
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.

TT--
  • 2,956
  • 1
  • 27
  • 46
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 4
    In addition, My style: Log.e(e.getClass().getName(), e.getMessage(), e); – Vikas Jan 20 '11 at 09:54
  • 29
    Beware using e.getMessage() - getMessage() may return null, depending upon the type of exception that has been thrown, which would in itself cause an exception as null is not supported as a message parameter in the Log methods. See [here](http://developer.android.com/reference/java/lang/Throwable.html#getMessage%28%29) – Uniqe Jun 08 '11 at 09:52
  • 1
    Besides, it makes more sense to describe what kind of code caused this exception in the second parameter. The message is already included in the output anyway. – EboMike Mar 16 '14 at 00:40
  • 1
    What @Unique says is very important. It is better to use Log as mentioned in the answer since it already prints the stack trace – Buddy Sep 27 '15 at 18:42
  • `Log.e("MYAPP", "exception", e);` In this MYAPP is for what? Please explain – Abdulla Sirajudeen Jun 21 '16 at 03:36
  • 1
    @SignoffTeamz You could just read the documentation. It's an identifier that shows up the log that makes it easier for you to filter the log and see which app (and which part of it) wrote the message. https://developer.android.com/reference/android/util/Log.html#e(java.lang.String, java.lang.String, java.lang.Throwable) – EboMike Jun 21 '16 at 14:53
56

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));
    }
George
  • 725
  • 7
  • 10
8
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.

Mark Storer
  • 15,672
  • 3
  • 42
  • 80
4
public String getStackTrace(Exception e){
  StringWriter sw = new StringWriter();
  PrintWriter pw = new PrintWriter(sw);
  e.printStackTrace(pw);
  return sw.toString();
}
2

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

Falmarri
  • 47,727
  • 41
  • 151
  • 191
1
try{
            ...
} catch (Exception e) {
     Log.e(e.getClass().getName(), e.getMessage(), e.getCause());
}
amin
  • 561
  • 6
  • 18
Frank Hou
  • 1,688
  • 1
  • 16
  • 11
1

if you want to print out stack trace without exception, you can create it by following command

(new Throwable()).printStackTrace();
Arvin Chen
  • 11
  • 2
1

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

toc777
  • 2,607
  • 2
  • 26
  • 37
0

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));
}
Daniel Viglione
  • 8,014
  • 9
  • 67
  • 101
0

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());

}
Arshak
  • 3,197
  • 1
  • 25
  • 33
0

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

e.stackTraceToString()
Dan
  • 180
  • 2
  • 4