Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I remove e.printStackTrace() from my code before publishing

I was reading the the Android Publishing docs and they said to remove all Log calls from my code. I have some calls to e.printStackTrace() in my code that can be printed as part of the normal running of my program (ie. if a file does not exist yet).

Should I also remove these calls?

like image 699
jax Avatar asked Jan 15 '10 14:01

jax


People also ask

Why we should not use e printStackTrace ()?

e. printStackTrace() is generally discouraged because it just prints out the stack trace to standard error. Because of this you can't really control where this output goes. The better thing to do is to use a logging framework (logback, slf4j, java.

What can I use instead of E printStackTrace?

Loggers should be used instead of printing the whole stack trace on stream. e. printStackTrace() prints a Throwable and its stack trace to stream which could inadvertently expose sensitive information. Loggers should be used instead to print Throwables, as they have many advantages.

Why do we use printStackTrace?

printStackTrace() is an indication that some exception is being swallowed and processing is allowed to proceed as if no problem every occurred.

Should we print stack trace?

printStackTrace(…) should never be called. Generic exceptions Error, RuntimeException, Throwable and Exception should never be thrown.


2 Answers

You shouldn't be using e.printStackTrace() directly anyway — doing so will send the info to the Android log without displaying which application (log tag) it came from.

As others have mentioned, continue to catch the Exception in question, but use one of the android.util.Log methods to do the logging. You could log only the message, but not the stack trace, or use verbose logging for the stack trace:

try {     Object foo = null;     foo.toString(); } catch (NullPointerException ex) {     Log.w(LOG_TAG, "Foo didn't work: "+ ex.getMessage());     Log.d(LOG_TAG, Util.stackTraceWriter(ex)); } 

You should strip DEBUG or VERBOSE log messages from your production builds. The easiest way is to use ProGuard to remove Log.[dv] calls from your code.

like image 72
Christopher Orr Avatar answered Sep 20 '22 14:09

Christopher Orr


If you allow an Exception to propagate up to the OS then the OS will log it and also pop up a Force Close window, killing your application. If you catch it, then you can prevent your application from being force closed.

If you want your users to have the ability to send you errors that they are getting, then I would log the stack trace. They can then send you the log via an app like Log Collector.

If you want to avoid the possibility of exposing your stack trace information to your users, then catch the exception and don't log it.

like image 45
Mark B Avatar answered Sep 20 '22 14:09

Mark B