简体   繁体   中英

handling errors outside of exceptions

I setup an exception handling class to log errors to the database which works really well but I was wondering if I can also somehow setup the application so any error outside of a try catch will call the same procedure somehow?

It works well most of the time and shows a screen with an error code to the user but I want to be able to use this friendly error screen each time but on the odd occasion an error outside of the try catch is thrown and it shows the normal asp.net error.

The catch I use:

catch (SqlException ex)
{
  ExceptionHandling.SQLException(ex, constPageID, constIsSiteSpecific);
}

It is possible to use the Global.asax's Application_Error method.

protected void Application_Error()
{
        Exception exception = Server.GetLastError();//Get the Last Error
        LogException(exception);//Custom Code
}

However be aware that if you do any error a user suffers will direct them onto your error page . It's often better to handle smaller errors on the page itself and present a simple message.

There are countless discussions on proper error handling - personally I like to catch everything the UI method which called it (OnPreRender, OnLoad OnEvent etc).

If you are developing an ASP.NET application, you can log unhandled exceptions in Global.asax in "Application_Error" method.

protected void Application_Error()
    {
        Exception exception = Server.GetLastError();
        // Clear the error
        Server.ClearError();

        // Log exception
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM