{"id":3625,"date":"2022-10-11T21:14:48","date_gmt":"2022-10-11T21:14:48","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=3625"},"modified":"2022-10-11T21:14:48","modified_gmt":"2022-10-11T21:14:48","slug":"handling-unhandled-exceptions-in-wpf","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/handling-unhandled-exceptions-in-wpf\/","title":{"rendered":"Handling &#8220;unhandled&#8221; exceptions in WPF"},"content":{"rendered":"<p><em>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind.<\/em><\/p>\n<p>None of us want our applications to just crash when an exception occurs so we often ensure we&#8217;ve got catch blocks around possible exceptions, but ofcourse, sometimes we either don&#8217;t care to handle an exception explicitly or we forget to code the catch block or the place the exception occurs such that it&#8217;s not possible to handle in such a structured way. In such scenarios we want to handle all &#8220;unhandled&#8221; exceptions at the application level.<\/p>\n<p>Let&#8217;s take a look at some of the ways to handle exceptions in a WPF application. Here&#8217;s a list some of those ways to handle &#8220;unhandled&#8221; exceptions.<\/p>\n<p><strong>AppDomain.UnhandledException<\/strong><\/p>\n<p>The <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.appdomain.unhandledexception(v=vs.110).aspx\" target=\"_blank\" rel=\"noopener\">AppDomain.UnhandledException<\/a> or more specifically <em>AppDomain.CurrentDomain.UnhandledException<\/em>.<\/p>\n<p><strong>Application.DispatcherUnhandledException<\/strong><\/p>\n<p>The <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.application.dispatcherunhandledexception(v=vs.110).aspx\" target=\"_blank\" rel=\"noopener\">Application.DispatcherUnhandledException<\/a> or more specifically the <em>Application.Current.DispatcherUnhandledException<\/em><\/p>\n<p><strong>Dispatcher.UnhandledException<\/strong><\/p>\n<p>The <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.windows.threading.dispatcher.unhandledexception(v=vs.110).aspx\" target=\"_blank\" rel=\"noopener\">Dispatcher.UnhandledException<\/a> or more specifically <em>Dispatcher.CurrentDispatcher.UnhandledException<\/em><\/p>\n<p><strong>AppDomain.FirstChanceException<\/strong><\/p>\n<p>The <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.appdomain.firstchanceexception(v=vs.110).aspx\" target=\"_blank\" rel=\"noopener\">AppDomain.FirstChanceException<\/a> or more specifically <em>AppDomain.CurrentDomain.FirstChanceException<\/em>.<\/p>\n<p><strong>TaskScheduler.UnobservedTaskException<\/strong><\/p>\n<p>The <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.threading.tasks.taskscheduler.unobservedtaskexception(v=vs.110).aspx\" target=\"_blank\" rel=\"noopener\">TaskScheduler.UnobservedTaskException<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: This post was written a while back but sat in draft. I&#8217;ve published this now, but I&#8217;m not sure it&#8217;s relevant to the latest versions etc. so please bear this in mind. None of us want our applications to just crash when an exception occurs so we often ensure we&#8217;ve got catch blocks around [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-3625","post","type-post","status-publish","format-standard","hentry","category-wpf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=3625"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3625\/revisions"}],"predecessor-version":[{"id":9593,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/3625\/revisions\/9593"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=3625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=3625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=3625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}