How to print stacktrace for an exception Android [duplicate]

I want to print the stack trace because at the moment I have this running.

} catch (IOException e) {
    throw new Error("Copying Failed");
}

And I have been told to print e.stacktrace();

How do I do this?


Solution 1:

} catch (IOException e) {
    Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}

And check the LogCat for the output.

Solution 2:

An other method, very useful :

try
{
...
}
catch (Exception e)
{
    Log.e(APP_TAG, "STACKTRACE");
    Log.e(APP_TAG, Log.getStackTraceString(e));
}

Solution 3:

In Android you should use the log methods that work nicely with the Logcat log viewer used by Android.

} catch (IOException e) {
   Log.e("YOUR ERROR TAG HERE", "Copying failed", e);
}

Using the Log.e method that takes a throwable as an argument you make sure that the log class will take the stacktrace and log it correctly to Logcat. If you use e.printStackTrace this will use the general Java logging methods and it will not appear correctly in Logcat and in some cases it will not be possible to double click on a class name in logcat to jump into the class and method mentioned in the stacktrace.

Print Stacktrace for a divide by zero will look like this:

11-21 20:55:47.360: W/System.err(989): java.lang.ArithmeticException: divide by zero
11-21 20:55:47.379: W/System.err(989):  at test.tabs.TabChooser.onCreate(TabChooser.java:15)
11-21 20:55:47.390: W/System.err(989):  at android.app.Activity.performCreate(Activity.java:4465)
11-21 20:55:47.410: W/System.err(989):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-21 20:55:47.410: W/System.err(989):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-21 20:55:47.420: W/System.err(989):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 20:55:47.420: W/System.err(989):  at android.os.Looper.loop(Looper.java:137)
11-21 20:55:47.420: W/System.err(989):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-21 20:55:47.430: W/System.err(989):  at java.lang.reflect.Method.invokeNative(Native Method)
11-21 20:55:47.430: W/System.err(989):  at java.lang.reflect.Method.invoke(Method.java:511)
11-21 20:55:47.430: W/System.err(989):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-21 20:55:47.430: W/System.err(989):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-21 20:55:47.430: W/System.err(989):  at dalvik.system.NativeStart.main(Native Method)

The exception is logged as a warning and the log tag is not very helpful.

Correct logging of a divide by zero will look like this:

11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356): Copying failed
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356): java.lang.ArithmeticException: divide by zero
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at test.tabs.TabChooser.onCreate(TabChooser.java:16)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.Activity.performCreate(Activity.java:4465)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.os.Looper.loop(Looper.java:137)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at android.app.ActivityThread.main(ActivityThread.java:4340)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at java.lang.reflect.Method.invokeNative(Native Method)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at java.lang.reflect.Method.invoke(Method.java:511)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-21 21:03:32.480: E/YOUR ERROR TAG HERE(1356):    at dalvik.system.NativeStart.main(Native Method)

The exception is correctly logged as an error with your log tag and log message.

Solution 4:

} catch (IOException e) {
    e.printStackTrace();
}