You must Sign In to post a response.
  • Category: ASP.NET

    Global Error Handling in ASP.NET

    I am using 4.0.Some time user forget to write try-catch block for perticular function in page.It throws exception.Client see yellow screen.I don't want like that.I have created Error.aspx to shows error message.When error occour on page,I am redirecting user to Error.aspx.How to do global error handling in .net
  • #765125
    You can handle application-level errors in Global.asax file as follows:

    void Application_Error(object sender, EventArgs e)// Code runs when unhandled error occurs in application
    // Get the last exception .
    Exception excp = Server.GetLastError();

    // OR
    // Show error message on error page
    Response.Write(exc.Message +"</p>\n");
    // Log exception and notify system
    ExceptionUtility.LogException(exc, "ErrorPage");
    Hope it helps.
    Shashikant Gurav

  • #765126
    Thanks shashikant,for ur help.I would like to ask u.Suppose there 2 user acessing 2 diff page,both got error,User1 get user1's error or user2's error.
    I am confuse because of following line.
    Exception excp = Server.GetLastError();

  • #765129
    whenever User1 get error on Page1, server will return Page1's error and whenever User2 get error on Page2, sever returns error from Page2.
    Though Global.asax is an Application specific file, user specific session are alloted by so for that session/instance this Server.GetLastError() will return specific error details.
    You can get detailed source of error using following code:
    Exception lastError = Server.GetLastError() as HttpException;
    string lastErrorType = lastError.GetType().ToString();
    string lastErrorMsg = lastError.Message;
    string lastErrorStackTrace = lastError.StackTrace;
    Or you can try:
    Exception lastError = (Server.GetLastError() as HttpException).InnerException;
    Shashikant Gurav

  • #765137
    When you are not write TRY...CATCH in page or function and if any code throws exception then it will goes in global.asax file in Application_Error event
    Look at the following code, This code example shows how to create an error handler in the Global.asax file that will catch all unhandled ASP.NET errors while processing a request — in other words, all the errors that are not caught with a Try/Catch block or in a page-level error handler. In the example, the handler transfers control to a generic error page named GenericErrorPage.aspx, which interprets the error and displays an appropriate message.
    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"))

    //Redirect HTTP errors to HttpError page

    // For other kinds of errors give the user some information
    // but stay on the default page
    Response.Write("<h2>Global Page Error</h2>\n");
    "<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");

    // Clear the error from the server

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765141
    1. Write the error log file in your application. It will be safe for know the error
    2. You can handle the error page in the web.config. If any error occurred in your application the page will be redirected to the error page which was designed by you.

    <customErrors defaultRedirect="GenericError.htm"
    <error statusCode="500"

    By Nathan
    Direction is important than speed

  • Sign In to post your comments