Debug.Assert vs Exception Throwing

前端 未结 8 1405
囚心锁ツ
囚心锁ツ 2020-12-12 10:49

I\'ve read plenty of articles (and a couple of other similar questions that were posted on StackOverflow) about how and when to use assertions, and I understood the

8条回答
  •  余生分开走
    2020-12-12 11:31

    Another nugget from Code Complete:

    "An assertion is a function or macro that complains loudly if an assumption isn't true. Use assertions to document assumptions made in code and to flush out unexpected conditions. ...

    "During development, assertions flush out contradictory assumptions, unexpected conditions, bad values passed to routines, and so on."

    He goes on to add some guidelines on what should and should not be asserted.

    On the other hand, exceptions:

    "Use exception handling to draw attention to unexpected cases. Exceptional cases should be handled in a way that makes them obvious during development and recoverable when production code is running."

    If you don't have this book you should buy it.

提交回复
热议问题