C#ASP.NET Page_Error不处理异常

时间:2019-04-09 17:59:03

标签: c# asp.net

事件Page_Error(对象发送者,EventArgs e)不处理期望。 Visual Studio正在处理异常。

我尝试通过Page_Load或button引发Exception,但仍然无法正常工作。我无法搜索有关此问题的任何信息。在我的书中显示了一个具有Page_Load,Page_Error,Button的简单示例,根据我的书,这应该可以工作。我猜想Web.config中缺少某些内容。

    void Page_Error(object sender, EventArgs e)
    {
        Response.Clear();
        Exception ex = Server.GetLastError();
        Response.Write("Something not yet");
        Response.Write(ex.Message);
        Server.ClearError();
    }

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Load event fired");
        throw new Exception();
    }

我希望抛出的异常将由Page_Error处理,而不是由Visual Studio处理。

3 个答案:

答案 0 :(得分:0)

您可以在global.asax中处理应用程序(Application_Error)级别的错误,并将请求转发到自定义错误页面。

这个SO线程可能看起来不错: Exception handling in ASP.NET Webforms

答案 1 :(得分:0)

尝试将您的Page_Error方法设置为受保护的

protected void Page_Error(object sender, EventArgs e)
    {
        Response.Clear();
        Exception ex = Server.GetLastError();
        Response.Write("Something not yet");
        Response.Write(ex.Message);
        Server.ClearError();
    }

答案 2 :(得分:0)

您抛出了错误类型的异常。抛出 ApplicationException 而不是 Exception