Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use assertion over exceptions in domain classes

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

like image 223
StackUnderflow Avatar asked Nov 25 '25 10:11

StackUnderflow


1 Answers

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.

like image 122
oefe Avatar answered Nov 28 '25 00:11

oefe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!