简体   繁体   English

处理异常以外的错误

[英]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? 我设置了一个异常处理类以将错误记录到数据库中,这确实很好,但是我想知道是否也可以以某种方式设置应用程序,以便try catch之外的任何错误都将以某种方式调用同一过程?

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. 它在大多数情况下都能正常工作,并向用户显示带有错误代码的屏幕,但我希望每次都能使用此友好的错误屏幕,但在奇怪的情况下,会抛出try catch之外的错误,并显示正常的asp.net错误。

The catch I use: 我用的渔获物:

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

It is possible to use the Global.asax's Application_Error method. 可以使用Global.asax的Application_Error方法。

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). 关于正确的错误处理,有无数的讨论-就我个人而言,我喜欢捕捉所有调用它的UI方法(OnPreRender,OnLoad OnEvent等)。

If you are developing an ASP.NET application, you can log unhandled exceptions in Global.asax in "Application_Error" method. 如果要开发ASP.NET应用程序,则可以在Global.asax中以“ Application_Error”方法记录未处理的异常。

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

        // Log exception
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PL / SQL异常和错误处理 - PL/SQL Exceptions and Errors Handling 使用CQRS处理中介管道中的错误/异常? - Handling errors/exceptions in a mediator pipeline using CQRS? 处理异常,错误和返回值时出现问题 - Problem with handling exceptions, errors and return values 处理错误-我应该在“事件驱动”应用程序中使用异常或事件吗? - Handling errors - should I use exceptions or events in an “event driven” application? 将 SQL Server 错误与其他类型的异常分开处理 - Handling SQL Server errors separately from other types of exceptions “通过捕获非特定的异常避免处理错误”是什么意思? - What it means to “Avoid handling errors by catching non-specific exceptions”? 处理异常 - Handling exceptions 处理服务外部发生的异常时,我是否可以访问dto请求? - Do I have access to the request dto when I'm handling exceptions occuring outside of services? C#处理异常和异常的数据错误和警告的最佳实践 - C# What is best practise for handling Data Errors and Warnings as opposed to Exceptions 处理异常,这是一个好方法吗? - Handling exceptions, is this a good way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM