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.
我哪里错了?
I have some custom error classes defined, each with their own functionality (not shown):
Exceptions.cs
public abstract class MyAppException : Exception {
//...
}
public class ValidationException : MyAppException {
//...
}
public class AccessDeniedException : MyAppException {
//...
}
Now in the codebehind of a blank page I have:
test.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
throw new AccessDeniedException();
}
My intention is to catch this at the application level:
Global.asax.cs
protected void Application_Error(object sender, EventArgs e) {
var exc = Server.GetLastError();
if (exc is MyAppException) ((MyAppException)exc).Log();
}
But by adding a breakpoint I find that exc is MyAppException
evaluates as false
.
Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 InnerException:
它是将实际异常包装到
HttpUnhandledException
中的System.Web.UI.Page.HandleError
方法。Try the InnerException:
It's the
System.Web.UI.Page.HandleError
method that wraps the actual exception into anHttpUnhandledException
.