异常处理准则
By S.F.
本文链接 https://www.kyfws.com/post/about-exception/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1378 个词 阅读量 0关于异常处理
原文来自 codeproject.com,虽然写了很多年了,而且是针对 .NET 的建议,但是别的语种都可以参考下处理异常的思路。
简单 google 翻译了下,发现挺不错的,直接贴了
-
Never do a “catch” exception and do nothing. If you hide an exception, you will never know if the exception happened or not. 永远不要执行“捕获”异常,但什么也不做。如果隐藏异常,您将永远不会知道该异常是否发生。
-
In case of exceptions, give a friendly message to the user, but log the actual error with all possible details about the error, including the time it occurred, method and class name etc. 如果发生异常,请向用户提供友好的消息,但要记录实际错误以及有关该错误的所有可能详细信息,包括发生时间,方法和类名等。
-
Always catch only the specific exception, not generic exception as well as system exceptions. 始终仅捕获特定的异常,而不捕获常规异常以及系统异常。
-
You can have an application level (thread level) error handler where you can handle all general exceptions. In case of an ‘unexpected general error’, this error handler should catch the exception and should log the error in addition to giving a friendly message to the user before closing the application, or allowing the user to ‘ignore and proceed’. 您可以具有应用程序级(线程级)错误处理程序,在其中可以处理所有常规异常。在发生“意外的一般错误”的情况下,此错误处理程序除了捕获关闭应用程序之前向用户提供友好消息或允许用户“忽略并继续”之外,还应捕获异常并记录错误。
-
Do not write try-catch in all your methods. Use it only if there is a possibility that a specific exception may occur. For example, if you are writing into a file, handle only FileIOException. 不要在所有方法中都编写try-catch。仅在有可能发生特定异常的情况下才使用它。例如,如果要写入文件,则仅处理FileIOException。
-
Do not write very large try-catch blocks. If required, write separate try-catch for each task you perform and enclose only the specific piece of code inside the try-catch. This will help you find which piece of code generated the exception and you can give specific error message to the user. 不要写很大的try-catch块。如果需要,请为您执行的每个任务编写单独的try-catch,并仅将特定的代码包含在try-catch中。这将帮助您查找哪个代码段生成了异常,并且可以向用户提供特定的错误消息。
-
You may write your own custom exception classes, if required in your application. Do not derive your custom exceptions from the base class SystemException. Instead, inherit from ApplicationException. 如果应用程序需要,您可以编写自己的自定义异常类。不要从基类SystemException派生自定义异常。而是从ApplicationException继承。
-
To guarantee resources are cleaned up when an exception occurs, use a try/finally block. Close the resources in the finally clause. Using a try/finally block ensures that resources are disposed even if an exception occurs. 为了确保在发生异常时清理资源,请使用try / finally块。关闭finally子句中的资源。使用try / finally块可确保即使发生异常也可以处理资源。
-
Error messages should help the user to solve the problem. Never give error messages like “Error in Application”, “There is an error” etc. Instead give specific messages like “Failed to update database. Make sure the login id and password are correct.” 错误消息应有助于用户解决问题。切勿提供诸如“应用程序中的错误”,“存在错误”等错误消息,而应提供诸如“未能更新数据库。请确保登录ID和密码正确”之类的特定消息。
-
When displaying error messages, in addition to telling what is wrong, the message should also tell what the user should do to solve the problem. Instead of message like “Failed to update database.” suggest what should the user do: “Failed to update database. Make sure the login id and password are correct.” 显示错误消息时,除了告诉您错误所在之外,该消息还应告诉用户解决问题应采取的措施。而不显示诸如“无法更新数据库”之类的消息。建议用户该怎么做:“无法更新数据库。确保登录ID和密码正确。”
-
Show short and friendly message to the user. But log the actual error with all possible information. This will help a lot in diagnosing problems. 向用户显示简短友好的消息。但是,请使用所有可能的信息记录实际错误。这将有助于诊断问题。
-
Define a global error handler in Global.asax to catch any exceptions that are not handled in code. You should log all exceptions in the event log to record them for tracking and later analysis. 在Global.asax中定义一个全局错误处理程序,以捕获未在代码中处理的所有异常。您应该将所有异常记录在事件日志中,以记录它们以进行跟踪和以后的分析。