Best Practice for Exception Handling in a Windows Forms Application?

前端 未结 16 2036
滥情空心
滥情空心 2020-11-29 14:20

I\'m currently in the process of writing my first Windows Forms application. I\'ve read a few C# books now so I\'ve got a relatively good understanding of what language feat

16条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 15:15

    There is an excellent code CodeProject article here. Here are a couple of highlights:

    • Plan for the worst*
    • Check it early
    • Don't trust external data
    • The only reliable devices are: the video, the mouse and keyboard.
    • Writes can fail, too
    • Code Safely
    • Don't throw new Exception()
    • Don't put important exception information on the Message field
    • Put a single catch (Exception ex) per thread
    • Generic Exceptions caught should be published
    • Log Exception.ToString(); never log only Exception.Message!
    • Don't catch (Exception) more than once per thread
    • Don't ever swallow exceptions
    • Cleanup code should be put in finally blocks
    • Use "using" everywhere
    • Don't return special values on error conditions
    • Don't use exceptions to indicate absence of a resource
    • Don't use exception handling as means of returning information from a method
    • Use exceptions for errors that should not be ignored
    • Don't clear the stack trace when re-throwing an exception
    • Avoid changing exceptions without adding semantic value
    • Exceptions should be marked [Serializable]
    • When in doubt, don't Assert, throw an Exception
    • Each exception class should have at least the three original constructors
    • Be careful when using the AppDomain.UnhandledException event
    • Don't reinvent the wheel
    • Don't use Unstructured Error Handling (VB.Net)

提交回复
热议问题