e.printStackTrace() works fine (i.e. prints my stacktrace to stderr) but Log.X fails to print a stacktrace at all.
For example:
} catch (IOException e) {
Log.e("Network", "Exception", e);
e.printStackTrace();
}
Output:
08-31 03:46:21.992: W/Network(13238): Exception
08-31 03:46:22.092: W/System.err(13238): java.net.UnknownHostException: Unable to resolve host "...": No address associated with hostname
08-31 03:46:22.204: W/System.err(13238): at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
08-31 03:46:22.222: W/System.err(13238): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-31 03:46:22.222: W/System.err(13238): at java.net.InetAddress.getAllByName(InetAddress.java:214)
To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.
The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.
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.
Turns out Android's Log.getStackTraceString which is used by Log.X swallows UnknownHostException. :(
From: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/util/Log.java#Log.getStackTraceString%28java.lang.Throwable%29
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return "";
}
// This is to reduce the amount of log spew that apps do in the non-error
// condition of the network being unavailable.
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
return "";
}
t = t.getCause();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
return sw.toString();
}
It's all very well reducing log spew but to not even tell me what my exception was is bad joojoo!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With