i have seen few codes where assert macro is used to make some checks on conditions. They used 'assert' so frequently (like in every destructor) that makes code looks too bad. My question is:

  • why people rely on assert so much. is it because is it easy to use or improves coding style (if so pls explain this point) or helps in debugging.
  • Can we use

exceptional handling or if-else condition check in stead of assert? How much it will be effective?

  • If assert is important, what are approprite situation to use them?
8 Years
Discussion Span
Last Post by majesticmanish

Assert is only one debugging tool, and does nothing if you compile the program for release instead of debug. Exception handling works in both debug and release compiles, asserts do not.


thanks for ur reply AD, so r u saying assert is useful for debugging purpose only, for release version of software assert should not be there??

And, from ur experience, if u have 2 choices 'Assert' or 'Exceptional Handling' which one u prefer and y??

double mysqrt ( int x ) {
  // code
  return result;

The whole point about assert is that you get to debug the code at the first detectable point of failure.
Imagine this code in some large and complicated program, where the only "bug report" you have is "sporadic #INF in output file".

Takes 5 seconds to find the problem with plenty of asserts. Or maybe a week of staring at the code, single-stepping in the debugger or what have you.

Asserts() attempt to verify the program by testing conditions which "must" be true assuming the programmer has done their job properly. Asserts which fail are bugs.

> from ur experience, if u have 2 choices 'Assert' or 'Exceptional Handling'
> which one u prefer and y??
Your first post was excellent, and now it's chat-speak :(

I don't see it as a mutually exclusive choice. Both have a place in a program.


thanks for clarification. I personally avoid use of assert and thats y i'm here to discuss this issue.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.