抓住HttpException的好主意?

cdu*_*dub 3 .net c# asp.net exception httpexception

我有时在output.aspx页面中出现以下错误:

异常详细信息:System.Web.HttpException:请求超时.

来源错误:

在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:

[HttpException(0x80004005):请求超时.]

抓住这个例外是一个好主意吗?我在哪里这样做,因为我的output.aspx.cs有一个Page_Load,该函数调用RunTable().我应该在功能内容周围放置一个try catch块吗?

Has*_*gha 5

在应用程序级别捕获异常

    void Application_Error(object sender, EventArgs e)
{
  // Code that runs when an unhandled error occurs

  // Get the exception object.
  Exception exc = Server.GetLastError();

  // Handle HTTP errors
  if (exc.GetType() == typeof(HttpException))
  {
    // The Complete Error Handling Example generates
    // some errors using URLs with "NoCatch" in them;
    // ignore these here to simulate what would happen
    // if a global.asax handler were not implemented.
      if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
      return;

    //Redirect HTTP errors to HttpError page
    Server.Transfer("HttpErrorPage.aspx");
  }

  // For other kinds of errors give the user some information
  // but stay on the default page
  Response.Write("<h2>Global Page Error</h2>\n");
  Response.Write(
      "<p>" + exc.Message + "</p>\n");
  Response.Write("Return to the <a href='Default.aspx'>" +
      "Default Page</a>\n");

  // Log the exception and notify system operators
  ExceptionUtility.LogException(exc, "DefaultPage");
  ExceptionUtility.NotifySystemOps(exc);

  // Clear the error from the server
  Server.ClearError();
}
Run Code Online (Sandbox Code Playgroud)

  • 虽然将中央错误处理程序作为_last ressort_很好,但我相信他应该更好地找到并修复异常的_cause_. (2认同)