zoukankan      html  css  js  c++  java
  • 异常处理准则

    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.

  • 相关阅读:
    innerHTML和innerText
    Function 构造器及其对象、方法
    构造函数
    jquery 获取及设置input各种类型的值
    在浏览器中输入URL并回车后都发生了什么?
    :after和:before的作用及使用方法
    使用JS监听键盘按下事件(keydown event)
    Javascript模块化编程(三):require.js的用法
    Javascript模块化编程(二):AMD规范
    Javascript模块化编程(一):模块的写法
  • 原文地址:https://www.cnblogs.com/Chinasf/p/1644727.html
Copyright © 2011-2022 走看看