title: Error handling not working asp.net
body: <p>I have the following code for handling my errors</p>
  
  <p>Global.asax.cs in Application_Error</p>
  
  <pre><code>Exception exception = Server.GetLastError();
  if (exception != null)
  {
      //Log the error
  }
  </code></pre>
  
  <p>However I also have custom error pages which I redirect to above as well as from the web.config - I did this because it seems that Application_Error does not catch all errors that occur?</p>
  
  <p>So in my error page on Page_Load I have this code</p>
  
  <pre><code>Exception exception = Server.GetLastError();                
  string exInnerExp = "";
  if (exception != null)
  {
      exInnerExp = exception.InnerException.ToString();
      //Log error
  }
  </code></pre>
  
  <p>This never works? I have no idea why, any help with this is much appreciated.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong>
  In my web.config I have the following code:</p>
  
  <pre><code>&lt;customErrors mode="RemoteOnly"&gt;
     &lt;error redirect="http://www.example.com/errorpages/404.aspx" statusCode="404" /&gt;
     &lt;error redirect="http://www.example.com/errorpages/siteerror.aspx" statusCode="500" /&gt;
  &lt;/customErrors&gt;
  </code></pre>
  
  <p>As well as</p>
  
  <pre><code>&lt;httpErrors&gt;
     &lt;remove statusCode="500" subStatusCode="-1" /&gt;
     &lt;remove statusCode="404" subStatusCode="-1" /&gt;
     &lt;error statusCode="404" prefixLanguageFilePath="" path="/errorpages/404.aspx" responseMode="ExecuteURL" /&gt;
     &lt;error statusCode="500" prefixLanguageFilePath="" path="/errorpages/siteerror.aspx" responseMode="ExecuteURL" /&gt;
      &lt;/httpErrors&gt;
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Initially I though that Application_Error was not being hit, but then I found a piece of code which was only actually running if Server.GetLastError() was not null. So now my problem has changed slightly, Server.GetLastError() is null in the Application_Error event. For example of where this can happen:</p>
  
  <p>I have a built project which works perfectly, not if I go to a UserControl and change a asp:TextBox's ID then the page will not load and throws and error, this error is not caught. </p>