开发者社区> 老朱教授> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

异常处理准则

简介:
+关注继续查看

  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,如需转载请自行联系原作者 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
异常的处理
异常处理的方式
27 0
中间件-异常处理
带着问题去思考!大家好!
63 0
异常处理
异常是指程序运行过程中所发生的不正常事件,他会中断正在运行的程序,解决异常的方法抓抛: try//执行可产生异常的代码 catch捕获异常 finally无论是否发生异常代码都执行 t...
599 0
C++ 异常处理
异常,让一个函数在遇到自己无法处理的问题时可以即时抛出,希望他到调用着直接或者间接的处理该异常。 错误的处理依然是一件很困难的事情,C++的异常机制为程序员提供了一种处理错误的方式,使程序员可以更自然的方式处理错误。
942 0
C++ 异常处理
From : http://www.uml.org.cn/c%2B%2B/201305272.asp From : http://bbs.csdn.net/topics/390269815 From : http://developer.51cto.com/art/201512/502598.htm From : http://www.cnblogs.com/ggjucheng/ar
2250 0
异常处理
AttributeError:试图访问一个对象没有属性,比如Foo.x但是Foo没有属性。 IoError:输入/输出异常,基本上是无法打开文件。 ImportError:无法引入模块或包,基本上是无法路径问题或名称错误。
558 0
+关注
3545
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载