{"id":491,"date":"2013-06-24T21:24:24","date_gmt":"2013-06-24T19:24:24","guid":{"rendered":"http:\/\/blog.ansuz.nl\/?p=491"},"modified":"2013-06-24T21:24:24","modified_gmt":"2013-06-24T19:24:24","slug":"global-exception-handling-in-android","status":"publish","type":"post","link":"http:\/\/blog.ansuz.nl\/index.php\/2013\/06\/24\/global-exception-handling-in-android\/","title":{"rendered":"Global Exception handling in Android"},"content":{"rendered":"<p>To be able to catch any exception you may encounter that isn&#8217;t part of a try-catch-clause, you can use the\u00a0<a href=\"http:\/\/developer.android.com\/reference\/java\/lang\/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)\" target=\"_blank\">Thread.setDefaultUncaughtExceptionHandler( Thread. UncaughtExceptionHandler )<\/a> method.<\/p>\n<p>In the handler you can then perform whatever action you want. Don&#8217;t forget to call &#8220;<a href=\"http:\/\/developer.android.com\/reference\/java\/lang\/System.html#exit(int)\" target=\"_blank\">System.exit()<\/a>&#8221; though,\u00a0because otherwise this won&#8217;t work!<\/p>\n<p>A simple example:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">java<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"java\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">public<\/span> <span style=\"color: #000000;font-weight: bold\">class<\/span> SomeActivity <span style=\"color: #000000;font-weight: bold\">extends<\/span> Activity <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">private<\/span> <span style=\"color: #000000;font-weight: bold\">static<\/span> <span style=\"color: #000000;font-weight: bold\">final<\/span> <span style=\"color: #003399\">String<\/span> LOG_TAG <span style=\"color: #339933\">=<\/span> <span style=\"color: #0000ff\">&quot;SomeActivity&quot;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; @Override<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">protected<\/span> <span style=\"color: #000066;font-weight: bold\">void<\/span> onCreate<span style=\"color: #009900\">&#040;<\/span>Bundle savedInstanceState<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">super<\/span>.<span style=\"color: #006633\">onCreate<\/span><span style=\"color: #009900\">&#040;<\/span>savedInstanceState<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #003399\">Thread<\/span>.<span style=\"color: #006633\">setDefaultUncaughtExceptionHandler<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000000;font-weight: bold\">new<\/span> <span style=\"color: #003399\">Thread<\/span>.<span style=\"color: #006633\">UncaughtExceptionHandler<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">public<\/span> <span style=\"color: #000066;font-weight: bold\">void<\/span> uncaughtException<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #003399\">Thread<\/span> paramThread, <span style=\"color: #003399\">Throwable<\/span> paramThrowable<span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style=\"color: #006633\">e<\/span><span style=\"color: #009900\">&#040;<\/span>LOG_TAG, <span style=\"color: #0000ff\">&quot;OMG! Uncaught Exception!&quot;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Without calling System.exit() this will not work.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #003399\">System<\/span>.<span style=\"color: #006633\">exit<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #cc66cc\">2<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>For some more details have a look at &#8220;<a href=\"http:\/\/stackoverflow.com\/questions\/4427515\/using-global-exception-handling-on-android\" target=\"_blank\">Using Global Exception Handling on android<\/a>&#8221; on <a href=\"http:\/\/stackoverflow.com\/\" target=\"_blank\">StackOverflow.com<\/a>.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>To be able to catch any exception you may encounter that isn&#8217;t part of a try-catch-clause, you can use the\u00a0Thread.setDefaultUncaughtExceptionHandler( Thread. UncaughtExceptionHandler ) method. In the handler you can then perform whatever action you want. Don&#8217;t forget to call &#8220;System.exit()&#8221; &hellip; <a href=\"http:\/\/blog.ansuz.nl\/index.php\/2013\/06\/24\/global-exception-handling-in-android\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-491","post","type-post","status-publish","format-standard","hentry","category-random"],"_links":{"self":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/comments?post=491"}],"version-history":[{"count":15,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/491\/revisions"}],"predecessor-version":[{"id":506,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/posts\/491\/revisions\/506"}],"wp:attachment":[{"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/media?parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/categories?post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ansuz.nl\/index.php\/wp-json\/wp\/v2\/tags?post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}