title: In MVC, why doesn't HandleError or Application_Error catch SqlExceptions?
body: <p>In MVC, why doesn't HandleError (extended or not) or Application_Error catch SqlExceptions?</p>
  
  <pre><code>    public override void OnException(ExceptionContext filterContext)
      {
          int httpcode = new HttpException(null, filterContext.Exception).GetHttpCode();
  
          if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
              return;
          if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
              return;
  
          var er = new OEB.DataAccess.ErrorHandlerRepository();
          er.LogException("Custom - " + filterContext.Exception.ToString());
  
          filterContext.ExceptionHandled = true;
          switch (httpcode)
          {
              case 404:
                  filterContext.Result = new RedirectResult("~/ErrorHandler/NotFound");
                  break;
              case 500:
                  filterContext.Result = new RedirectResult("~/ErrorHandler/ServerError");
                  break;
              default:
                  filterContext.Result = new RedirectResult("~/ErrorHandler/General");
                  break;
          }            
      }
  </code></pre>
  
  <p>I register this in Filter.Config:</p>
  
  <pre><code>public class FilterConfig
  {
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
      {
          filters.Add(new CustomErrorHandlerAttribute());
      }
  }
  </code></pre>
  
  <p>I am using Dapper, and trying to catch the SQLException from an invalid insert(AspNetRoles2 does not exist), and get the normal yellow screen:</p>
  
  <pre><code>    public bool CreateRole(string roleName)
      {
          int rowsAffected = Execute("INSERT into AspNetRoles2 (RoleName) VALUES (@roleName)", new { roleName = roleName });
          return rowsAffected == 1 ? true : false;
      }
  
      protected int Execute(string sp, object parameters = null)
      {
              using (var scope = new TransactionScope())
              {
                  using (var conn = new SqlConnection(secConnStr))
                  {
                      rowsAffectedCount = conn.Execute(sp, parameters);
                  }
                  scope.Complete();
              }
       }
  </code></pre>