异常处理准则

简介:

  1. Never do a "catch" exception and do nothing. If you hide an exception, you will never know if the exception happened or not.
  2. 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.
  3. Always catch only the specific exception, not generic exception as well as system exceptions.
  4. 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'.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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."
  10. 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."
  11. 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.
  12. 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.



  13. 本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2010/01/12/1644727.html,如需转载请自行联系原作者 
相关文章
|
6月前
|
存储 程序员 编译器
C错误处理的研究
C错误处理的研究
43 3
|
Java
知识单元四 异常处理
知识单元四 异常处理
119 1
|
SQL Java 编译器
java异常分类,异常处理,面试中常见异常问题!
java异常分类,异常处理,面试中常见异常问题!
190 0
java异常分类,异常处理,面试中常见异常问题!
|
C语言 C++
【异常机制高级讲解】异常与多态综合案例
【异常机制高级讲解】异常与多态综合案例
107 0
|
程序员 C++ C语言
《C++编程规范:101条规则、准则与最佳实践》——导读
许多糟糕的编程规范都是由一些没有很好地理解语言、没有很好地理解软件开发或者试图标准化过多东西的人制定的。糟糕的编程规范会很快丧失可信度,如果程序员不喜欢或者不同意其中一些糟糕的准则,那么即使规范中有一些合理的准则,也可能被不抱幻想的程序员所忽略,这还是最好的情况,最坏的情况下,糟糕的标准可能真会被强
1881 0
|
Java
加深理解Java异常概念并熟记5个最常见的运行时异常
加深理解Java异常概念并熟记5个最常见的运行时异常
281 0
加深理解Java异常概念并熟记5个最常见的运行时异常