[ACCEPTED]-Should I remove e.printStackTrace() from my code before publishing-production
You shouldn't be using e.printStackTrace()
directly anyway 11 — doing so will send the info 10 to the Android log without displaying which 9 application (log tag) it came from.
As others 8 have mentioned, continue to catch the Exception
in 7 question, but use one of the android.util.Log
methods to 6 do the logging. You could log only the 5 message, but not the stack trace, or use 4 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 3 should strip DEBUG
or VERBOSE
log messages from your 2 production builds. The easiest way is to 1 use ProGuard to remove Log.[dv]
calls from your code.
If you allow an Exception to propagate up 12 to the OS then the OS will log it and also 11 pop up a Force Close window, killing your 10 application. If you catch it, then you 9 can prevent your application from being 8 force closed.
If you want your users to 7 have the ability to send you errors that 6 they are getting, then I would log the stack 5 trace. They can then send you the log via 4 an app like Log Collector.
If you want to avoid the possibility 3 of exposing your stack trace information 2 to your users, then catch the exception 1 and don't log it.
I would use Log class for message out put. For 5 logs that you think are important to stay 4 in the app - use Log.i for errors warning 3 - Log.e Log.w For you debug Log.d - and 2 that you can turnoff on base on if your 1 application is in debug mode.
http://developer.android.com/reference/android/util/DebugUtils.html
Well printStackTrace()
will log it into the OS, causing your 2 andorid (or computer) app to terminate (force 1 close), instead, do something like this:
public void nullPointerExceptionCauser()
{
try
{
Object example = null;
example.toString();
}
catch (Exception e)
{
Logger.log(Level.SEVERE, "Caught Exception: {0}", e.getStackTrace());
}
}
in my modest opinion (I'm not an Android developer)
It should be nice. I don't know the logging 5 options for Android but I'm sure you have 4 some configurable thing to output (or not) your 3 traces.
And if you don't do printStackTrace() Android 2 will not be doing the dirty work of ignoring 1 it.
:)
It's only a good-feeling (style) thing.
If you want to be secure i.e. not allow 2 anyone snooping to read exception logs you 1 can do something like
private void hideExceptionsInReleaseMode()
{
final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
if(!BuildConfig.DEBUG)
{
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread thread, Throwable ex)
{
defaultHandler.uncaughtException(thread, new RuntimeException("Something went wrong :p"));
}
});
}
}
In order to use printStackTrace
in a safer way I would 1 use StringWrite
and PrintWriter
:
...
catch (final Exception e)
{
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
Log.e("TAG", sw.toString());
}
Or alternatively:
catch (final Exception e)
{
Log.e(TAG, Log.getStackTraceString(e));
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.