【发布时间】:2013-05-01 04:54:55
【问题描述】:
即使发生未处理的异常,也会调用 Page_Unload。我需要处理这种情况。
当变量在 Page_Unload 中的状态不正确时,我有一个变量状态验证抛出异常。该异常稍后由Application_Error 在Global.asax 中处理。当另一个异常已经发生时,我需要抑制抛出异常。
页面:
public partial class _Default : System.Web.UI.Page
{
private int tst = 0;
protected void Page_Load(object sender, EventArgs e)
{
tst = tst / tst; //causes "Attempted to divide by zero."
tst = 1;
}
protected void Page_Unload(object sender, EventArgs e)
{
if (tst == 0) throw new Exception("Exception on unload");
}
}
全球.asax:
void Application_Error(object sender, EventArgs e)
{
// Get the error details
Exception lastErrorWrapper = Server.GetLastError();
System.Diagnostics.Debug.Print(lastErrorWrapper.Message);
}
我需要在Global.asax 中得到 “试图除以零。” 但我得到 “卸载时出现异常”
给出的示例已大大简化。真实情况包括一个用户控件和一个条件编译。
我不能解决Page_Load 中的情况(例如通过捕获异常)。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# asp.net error-handling global-asax