When to use assertion over exceptions in domain classes

前端 未结 3 1923
灰色年华
灰色年华 2021-01-06 14:37

Are there any situations when you would use assertion instead of exceptions-handling inside domain classes...

3条回答
  •  不思量自难忘°
    2021-01-06 15:23

    Use exceptions for parameter validation and other checks which verify that the users of you classes use them as intended.

    Use assertions for internal consistency checks, i.e. to indicate you screwed up, not the user of your class.

    Thus, if users of your class see an assertion failure, they know it is (probably) an internal error in your code, not in their use of your code. On the other hand, if the get parameter validation exception, they know it's their fault.

提交回复
热议问题