title: Global exception handler with UnhandledExceptionEventArgs not working
body: <p>After reading <a href="https://msdn.microsoft.com/en-us/library/system.unhandledexceptioneventargs(v=vs.110).aspx#Anchor_5" rel="nofollow noreferrer">this</a> MSDN page, I've created a global exception handler in my .net class library, for logging purposes, which looks like this:</p>
  
  <pre><code>    static void OnException(object sender, UnhandledExceptionEventArgs args)
      {
          Exception ex = (Exception)args.ExceptionObject;
          Logging.LogException(ex);
      }
  </code></pre>
  
  <p>But then if I <code>throw new UnauthorizedAccessException()</code> or <code>throw new Exception()</code> from a method, this does not catch it at all.</p>
  
  <p>The MSDN page says:</p>
  
  <blockquote>
    <p>UnhandledExceptionEventArgs provides access to the exception object
    and a flag indicating whether the common language runtime is
    terminating. The UnhandledExceptionEventArgs is one of the parameters
    passed into UnhandledExceptionEventHandler for the
    <strong>AppDomain.UnhandledException</strong> event</p>
  </blockquote>
  
  <p>I believe what I'm doing falls under the AppDomain (and not <strong>ThreadException</strong>)? What am I doing wrong here?</p>
  
  <p>PS. I'm trying to avoid a <code>try-catch</code> block, since apparently <a href="https://stackoverflow.com/a/4827646/2025666">it's bad practice</a>. This class library is called from a windows service which runs periodically so I'd rather not let it 'crash' to avoid memory leaks due to unforeseen exceptions and would prefer to monitor the event logs regularly.</p>