Don’t Use Assert() If You Don’t Mean It (C++)

I found the hard way that assert() in C/C++ exists hard. This post explains that you get a SIGABRT . So, if you want your code to be testable, you better throw an exception instead.

I defined a


So, in a code like this:

You’ll get an exception with the message “1 > 2”. Useful. You can use this sort of behaviour in e.g. unit tests (bonus: exceptions are good for you!)

A little experiment: If you find this post and ad below useful, please check the ad out :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top