I am currently adding handling to a segment of code so that it will not crash. Currently it has each step then an ASSERT statement to make sure nothing went wrong in the previous step. If something went wrong in one of these steps something did go very wrong. The program should stop.
Though in release mode the program hits a assert, happily carries on its way and crashes.
To fix this I have wrapped the method in a try/catch block and throw errors where the asserts used to be. This should catch all the errors we track and others we do not.
Now my question is, should I still be using asserts to notify the programmer that this should not have happened? Or take them out now that it will not crash because of the catch block (where I cleanup the object)?
Or alternatively should I just throw an assert in the catch block instead of with each throw statement?
In order to catch the assertion error, we need to declare the assertion statement in the try block with the second expression being the message to be displayed and catch the assertion error in the catch block.
In order to handle the assertion error, we need to declare the assertion statement in the try block and catch the assertion error in the catch block.
An assertion is a statement in Java which ensures the correctness of any assumptions which have been done in the program. When an assertion is executed, it is assumed to be true. If the assertion is false, the JVM will throw an Assertion error. It finds it application primarily in the testing purposes.
An assert is inappropriate because the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError .
try
& catch
and assert
has completely different purposes (at least in my view).
try
and catch
are used to cope with expected error conditions (user supplied a filename that doesn't exist, calling new couldn't allocate memory, user entered invalid input of some sort, etc).
The purpose of assert
is the guarantee that the programmer doesn't make mistakes. Hopefully, when running the code in release, you'd have already covered those alternatives, and know that the code is "good". Typical examples of assert
would be some pointer the user should not supply as NULL
is indeed not NULL
, or a linked list has the expected number of nodes [e.g. you count the number of nodes before remove_node
, and check that the number of nodes is indeed exactly one less].
If you are NOT 100% certain (or at least 98.75% certain or whatever level you decide is "good enough") that you have tested all of your code, then you should not make the release - or if you do, get your quality assurance manager to sign off that "we haven't done enough testing, but we are OK with that".
try
and catch
should only be used for things your program can reliably recover from. Not to catch "programmer wrote stupid code that causes a crash".
Edit to clarify the actual answer:
In other words, yes, you should use assert
for things that you don't expect to ever happen in the code [at least if the programmer is not doing things wrong] - this includes for example checking that the vector inside a vector for a matrix is indeed a square when you expect to have a square matrix, for example, or that pointers are not NULL
[except where you expect them to be - and perhaps even check that they ARE NULL
when they SHOULD be].
You should also use error checking with try
/catch
and throw
when things go wrong that could well happen during the running of the actual program in "real life" - disk full (or read-only), files not existing, not enough memory, things of that sort.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With