[英]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.