How to print stacktrace for an exception Android [duplicate]
Asked Answered
B

5

35

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?

Benzyl answered 21/11, 2011 at 19:37 Comment(0)
K
86
} catch (IOException e) {
    Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
}

And check the LogCat for the output.

Kazmirci answered 21/11, 2011 at 19:42 Comment(5)
Is this preferable over System.err?Voyeurism
@Igor G.: Yes, Igor, this is preferable over System.err. If you use simply System.err, then you will not be able to see your app tag in the LogCat output.Kazmirci
I only get java.lang.NullPointerException but that is not a stacktrace, that is just the error !, how can I get the full trace ?Despoliation
@FranciscoCorralesMorales: Did you pass THREE parameters to Log.e? If you only pass TWO parameters, e.g. Log.e("tag", e); or Log.e("tag", "message" + e); then you won't get the trace. Must be Log.e("tag", "message", e);.Amaya
Somehow never knew this override of Log methods existed. Woooooo!Fredela
P
31

An other method, very useful :

try
{
...
}
catch (Exception e)
{
    Log.e(APP_TAG, "STACKTRACE");
    Log.e(APP_TAG, Log.getStackTraceString(e));
}
Puncture answered 6/1, 2012 at 10:5 Comment(2)
where did you set APP_TAG ? or you didn't ?Despoliation
@FranciscoCorralesMorales: APP_TAG is just a common convention for naming your project's tag with a constant variable. You can define it to whatever you want your application tag name to be.Merilee
L
14

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.

Lora answered 21/11, 2011 at 19:42 Comment(0)
S
9
} catch (IOException e) {
    e.printStackTrace();
}
Stanzel answered 21/11, 2011 at 19:39 Comment(0)
E
0

It is most likely you were requested to print the stack trace via e.printStackTrace();...

} catch (IOException e) {
    e.printStackTrace();
    throw new Error("Copying Failed");
}
Embattle answered 21/11, 2011 at 19:40 Comment(1)
This doesn't print to logcat. This returns Kotlin.Unit when printed in logcatOp

© 2022 - 2024 — McMap. All rights reserved.