良好的错误处理实践 [英] Good error handling practice
问题描述
与任何.net项目一样,我发现最好的方法是只捕捉特定的错误类型,如果他们可能发生在给定页面上。
例如,您可以为给定输入的用户捕获格式化异常(只是使JavaScript验证失败,并且您没有使用tryparse),但始终保持捕获顶级异常对于全局错误处理程序。
尝试
{
//可能会在此错误的代码
}
catch(FormatException ex)
{
//代码告诉用户他们的错误
//所有其他错误将被处理
//由全局错误处理程序
}
您可以使用开源 elmah (错误日志记录模块和处理程序)为ASP.Net做这个顶级/全局错误捕捉你,如果你想要的。
使用 elmah 可以创建通过简单的配置Web界面可以查看的错误日志。您还可以过滤不同类型的错误,并针对不同的错误类型自定义错误页面。
What is a good error handling practice for an asp.net site? Examples? Thanks!
As with any .net project I find the best way is to only catch specific error types if they are may to happen on the given page.
For example you could catch Format Exceptions for a users given input (just incase JavaScript validation fails and you have not use tryparse) but always leave the catching of the top level Exception to the global error handler.
try
{
//Code that could error here
}
catch (FormatException ex)
{
//Code to tell user of their error
//all other errors will be handled
//by the global error handler
}
You can use the open source elmah (Error Logging Modules and Handlers) for ASP.Net to do this top level/global error catching for you if you want.
Using elmah it can create a log of errors that is viewable though a simple to configure web interface. You can also filter different types of errors and have custom error pages of your own for different error types.
这篇关于良好的错误处理实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!