{"id":1786,"date":"2022-08-30T15:19:26","date_gmt":"2022-08-30T15:19:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/need-to-handle-uncaught-exception-and-send-log-file-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:26","modified_gmt":"2022-08-30T15:19:26","slug":"need-to-handle-uncaught-exception-and-send-log-file-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/need-to-handle-uncaught-exception-and-send-log-file-collection-of-common-programming-errors\/","title":{"rendered":"Need to handle uncaught exception and send log file-Collection of common programming errors"},"content":{"rendered":"<p>Here&#8217;s the complete solution (almost: I omitted the UI layout and button handling) &#8211; derived from a lot of experimentation and various posts from others related to issues that came up along the way.<\/p>\n<p>There are a number of things you need to do:<\/p>\n<ol>\n<li>Handle uncaughtException in your Application subclass.<\/li>\n<li>After catching an exception, start a new activity to ask the user to send a log.<\/li>\n<li>Extract the log info from logcat&#8217;s files and write to your own file.<\/li>\n<li>Start an email app, providing your file as an attachment.<\/li>\n<li>Manifest: filter your activity to be recognized by your exception handler.<\/li>\n<li>Optionally, setup Proguard to strip out Log.d() and Log.v().<\/li>\n<\/ol>\n<p>Now, here are the details:<\/p>\n<p>(1 &amp; 2) Handle uncaughtException, start send log activity:<\/p>\n<pre><code>public class MyApplication extends Application\n{\n  public void onCreate ()\n  {\n    \/\/ Setup handler for uncaught exceptions.\n    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()\n    {\n      @Override\n      public void uncaughtException (Thread thread, Throwable e)\n      {\n        handleUncaughtException (thread, e);\n      }\n    });\n  }\n\n  public void handleUncaughtException (Thread thread, Throwable e)\n  {\n    e.printStackTrace(); \/\/ not all Android versions will print the stack trace automatically\n\n    Intent intent = new Intent ();\n    intent.setAction (\"com.mydomain.SEND_LOG\"); \/\/ see step 5.\n    intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); \/\/ required when starting from Application\n    startActivity (intent);\n\n    System.exit(1); \/\/ kill off the crashed app\n  }\n}\n<\/code><\/pre>\n<p>(3) Extract log (I put this an my SendLog Activity):<\/p>\n<pre><code>private String extractLogToFile()\n{\n  PackageManager manager = this.getPackageManager();\n  PackageInfo info = null;\n  try {\n    info = manager.getPackageInfo (this.getPackageName(), 0);\n  } catch (NameNotFoundException e2) {\n  }\n  String model = Build.MODEL;\n  if (!model.startsWith(Build.MANUFACTURER))\n    model = Build.MANUFACTURER + \" \" + model;\n\n  \/\/ Make file name - file must be saved to external storage or it wont be readable by\n  \/\/ the email app.\n  String path = Environment.getExternalStorageDirectory() + \"\/\" + \"MyApp\/\";\n  String fullName = path + ;\n\n  \/\/ Extract to file.\n  File file = new File (fullName);\n  InputStreamReader reader = null;\n  FileWriter writer = null;\n  try\n  {\n    \/\/ For Android 4.0 and earlier, you will get all app's log output, so filter it to\n    \/\/ mostly limit it to your app's output.  In later versions, the filtering isn't needed.\n    String cmd = (Build.VERSION.SDK_INT<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:31:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the complete solution (almost: I omitted the UI layout and button handling) &#8211; derived from a lot of experimentation and various posts from others related to issues that came up along the way. There are a number of things you need to do: Handle uncaughtException in your Application subclass. After catching an exception, start [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1786","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1786"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1786\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}