Question:
C++: Assert vs. Exceptions?
tonee47
2008-04-26 21:23:19 UTC
Hi there,

I have a basic understanding of the difference between Asserts and Exceptions in C++, but I guess I need more of an in-depth explanation as to why I would use one over the other.
I read about Asserts being used in debug rather than prod builds... how would one remove ALL Assert statements from a debug build?
Thanks
Three answers:
The Phlebob
2008-04-26 21:48:09 UTC
You don't have to remove Asserts. They automatically don't get compiled unless _DEBUG is set.



Hope that helps.
ambivalent
2008-04-27 04:43:42 UTC
Been a long, long time since I worked in C++. But some compilers or precompilers for other languages may actually be designed to look for Assert statements and remove them automatically on a production build. If not you can always put conditional pre-processor directives around each Assert statement (a bit of a drag if you didn't do it from the start though, and a pain to remember). Something like:



#if ASSERTS_ON

Assert("just checking that the flow never gets to this line.");

#endif



Then define or don't defined ASSERTS_ON based on your build type (debug or production).



Exceptions are a different matter entirely. In general most apps of any complexity can hardly do without them for handling unexpected outcomes/problems. However people and projects have many different strategies and guidelines for Exception usage: some like to get crazy specific with defining different Exception types for everything, some like to stick to the basics, some might require only one try/catch block around the whole body of each method, etc.
Craig R
2008-04-27 04:31:13 UTC
An assert makes a statement about a condition you believe to be true at a certain point in your program. If the statement is false, the assert fires and stops program execution.



Asserts are VERY useful debugging tools. There's no good reason to remove them from a debug build unless they're no longer true. It would be trivial to do a search/replace and take them all out but that usually indicates you're doing something wrong. For example if you're taking them out because they keep firing but they don't mean anything, then you're writing bad assertions.



An exception is thrown when something occurs in your code that is an error and you need to immediately jump from the current execution point to your exception handler. Exceptions are unusual circumstances that require that your code no longer continue to execute, unlike an expected "error" where you might be able to recover.



For example if you ask the user to enter an integer in an input field and they enter "ABC", you can handle that in your code without throwing an exception. If, on the other hand, your program writes its configuration data to a file, then reads that file back in and discovers it's scrambled beyond recognition, then it's most likely that program execution needs to stop. That's the time to throw an exception.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...