今天测试时发现,在无网情况下,在 try catch 内调用 Log 如下方法打印异常信息,无输出。同样也没有成功写入到本地文件,很奇怪。 之后看了下源码,发现系统对于 UnknownHostException 异常做了特殊限制,可能是出于信息安全方面的考虑,不会对此异常的信息进行打印输出。来看下代码: 而为何本地文件也没有写入成功呢?这是因为在写入的时候,我调用了 getStackTraceString 方法来获取异常追踪信息,而同样系统也对该方法进行了限制: 再来看下源码,真是奇怪的知识又增加了。所以我们要做好适配,针对UnknownHostException 可以通过输出 e.toString() 来避免这个问题。
现象
public static int e(String tag, String msg, Throwable tr) { return printlns(LOG_ID_MAIN, ERROR, tag, msg, tr); }
分析
if (tr != null) { // 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) { break; } if (t instanceof DeadSystemException) { lbbw.println("DeadSystemException: The system died; " + "earlier logs will point to the root cause"); break; } t = t.getCause(); } if (t == null) { tr.printStackTrace(lbbw); } }
Throwable trace = current.getTrace(); if (trace != null) { // todo:要适配 UnknownHostException msg = Log.getStackTraceString(trace); }
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 FastPrintWriter(sw, false, 256); tr.printStackTrace(pw); pw.flush(); return sw.toString(); }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算