Design by contract using assertions or exceptions? -


when programming contract function or method first checks whether preconditions fulfilled, before starting work on responsibilities, right? 2 prominent ways these checks assert , exception.

  1. assert fails in debug mode. make sure crucial (unit) test separate contract preconditions see whether fail.
  2. exception fails in debug , release mode. has benefit tested debug behavior identical release behavior, incurs runtime performance penalty.

which 1 think preferable?

see releated question here

disabling assert in release builds saying "i never have issues whatsoever in release build", not case. assert shouldn't disabled in release build. don't want release build crashing whenever errors occur either, you?

so use exceptions , use them well. use good, solid exception hierarchy , ensure catch , can put hook on exception throwing in debugger catch it, , in release mode can compensate error rather straight-up crash. it's safer way go.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -