+ 1
Assert Vs Exception
What to choose between assert and exception ? I am on C++ and it seems this is applicable to most of the developers. Assert is something which is not expected , then why should we allow to proceed them further? And if we are not allowing code flow to proceed further, how user gets information without exception handling?
4 Respuestas
+ 6
Ketan Lalcheta ,
from my point of view (talking about python), *assert* is not meant for use in final code, but for testing and debugging during.
see this tutorial from realpython:
https://realpython.com/python-assert-statement/
+ 6
Assert is for unit tests and should't be a part of any production code. You can create a small separate project to handle unit tests where you use assert.
+ 2
Sounds good.
However, My only concern is about use case in bigger code base.
For example, I am doing development and never got an assert as it is specific to work flow.Same code is passed to tester and as asserts are disabled in release build, he will get crash. How asserts are helping here. Would not it been great if it was exception itself ?
so, never have assert and go with exception always.right ?
+ 1
Let says a function generates random numbers between 0 and 1 but you just want the range of 0 and 0.999999, you're not quite sure wether 1 would eventually be part of the result.
You can just assert that function