{"id":1761,"date":"2022-08-30T15:19:13","date_gmt":"2022-08-30T15:19:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/01\/rethrow-uncaughtexceptionhandler-exception-after-logging-it-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:13","modified_gmt":"2022-08-30T15:19:13","slug":"rethrow-uncaughtexceptionhandler-exception-after-logging-it-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rethrow-uncaughtexceptionhandler-exception-after-logging-it-collection-of-common-programming-errors\/","title":{"rendered":"Rethrow UncaughtExceptionHandler Exception after Logging It-Collection of common programming errors"},"content":{"rendered":"<p>Apologies, not an Android expert &#8211; but looks like you can&#8217;t throw ex because your method signature &#8220;void uncaughtException(Thread, Throwable)&#8221; doesn&#8217;t declare that it &#8220;throws&#8221; anything.<\/p>\n<p>Assuming you&#8217;re overriding an API interface and (a) can&#8217;t modify this signature and (b) don&#8217;t want to because you&#8217;d be throwing it out of context, could you instead use a decorator pattern and basically subclass the default UncaughtExceptionHandler implementation to log your message and then let it carry on processing as usual?<\/p>\n<p>Edit: untested, but this might look a bit like:<\/p>\n<pre><code>    final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler();\n    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {\n        @Override\n        public void uncaughtException(Thread thread, Throwable ex) {\n            \/\/ your code \n            AnalyticsUtils.getInstance(MainApplication.this).trackEvent(\n                    \"Errors\",                       \/\/ Category\n                    \"MainActivity\",                 \/\/ Action\n                    \"Force Close: \"+ex.toString(),  \/\/ Label\n                    0);                             \/\/ Value\n            AnalyticsUtils.getInstance(MainApplication.this).dispatch();\n            Toast.makeText(MainApplication.this, \"Snap! Something broke. Please report the Force Close so I can fix it.\", Toast.LENGTH_LONG);\n\n            \/\/ carry on with prior flow\n            subclass.uncaughtException(thread, ex);\n        }\n    });\n<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-12-01 09:11:11. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Apologies, not an Android expert &#8211; but looks like you can&#8217;t throw ex because your method signature &#8220;void uncaughtException(Thread, Throwable)&#8221; doesn&#8217;t declare that it &#8220;throws&#8221; anything. Assuming you&#8217;re overriding an API interface and (a) can&#8217;t modify this signature and (b) don&#8217;t want to because you&#8217;d be throwing it out of context, could you instead use [&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-1761","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1761","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=1761"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1761\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}