2011-10-15 59 views
1

我有些自定义错误类定义,每个都有自己的功能(未示出):ASP.NET错误处理 - 未检测到正确的错误类型中的Global.asax

Exceptions.cs

public abstract class MyAppException : Exception { 
    //... 
} 
public class ValidationException : MyAppException { 
    //... 
} 
public class AccessDeniedException : MyAppException { 
    //... 
} 

现在在一个空白页的代码隐藏我:

test.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    throw new AccessDeniedException(); 
} 

我的目的是要抓住这个在应用层面:

的Global.asax.cs

protected void Application_Error(object sender, EventArgs e) { 
    var exc = Server.GetLastError(); 
    if (exc is MyAppException) ((MyAppException)exc).Log(); 
} 

但通过添加一个断点我发现exc is MyAppException评估为false

我哪里错了?

回答

2

尝试的InnerException:

if (exc.InnerException is MyAppException) 

这是System.Web.UI.Page.HandleError方法封装了实际的异常为HttpUnhandledException