Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 31869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:40:22+00:00 2026-05-10T13:40:22+00:00

If I use assert() and the assertion fails then assert() will call abort() ,

  • 0

If I use assert() and the assertion fails then assert() will call abort(), ending the running program abruptly. I can’t afford that in my production code. Is there a way to assert in runtime yet be able to catch failed assertions so I have the chance to handle them gracefully?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:40:22+00:00Added an answer on May 10, 2026 at 1:40 pm

    Yes, as a matter of fact there is. You will need to write a custom assert function yourself, as C++’s assert() is exactly C’s assert(), with the abort() ‘feature’ bundled in. Fortunately, this is surprisingly straightforward.

    Assert.hh

    template <typename X, typename A> inline void Assert(A assertion) {     if( !assertion ) throw X(); } 

    The above function will throw an exception if a predicate doesn’t hold. You will then have the chance to catch the exception. If you don’t catch the exception, terminate() will be called, which will end the program similarly to abort().

    You may wonder what about optimizing away the assertion when we’re building for production. In this case, you can define constants that will signify that you’re building for production and then refer to the constant when you Assert().

    debug.hh

    #ifdef NDEBUG     const bool CHECK_WRONG = false; #else     const bool CHECK_WRONG = true; #endif 

    main.cc

    #include<iostream>  struct Wrong { };  int main() {     try {         Assert<Wrong>(!CHECK_WRONG || 2 + 2 == 5);         std::cout << 'I can go to sleep now.\n';     }     catch( Wrong e ) {         std::cerr << 'Someone is wrong on the internet!\n';     }      return 0; } 

    If CHECK_WRONG is a constant then the call to Assert() will be compiled away in production, even if the assertion is not a constant expression. There is a slight disadvantage in that by referring to CHECK_WRONG we type a little more. But in exchange we gain an advantage in that we can classify various groups of assertions and enable and disable each of them as we see fit. So, for example we could define a group of assertions that we want enabled even in production code, and then define a group of assertions that we only want to see in development builds.

    The Assert() function is equivalent to typing

    if( !assertion ) throw X(); 

    but it clearly indicates the intent of the programmer: make an assertion. Assertions are also easier to grep for with this approach, just like plain assert()s.

    For more details on this technique see Bjarne Stroustrup’s The C++ Programming Language 3e, section 24.3.7.2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 185k
  • Answers 185k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Patches, that git diff yields, are correctly processed by patch… May 12, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer Because it has the nice property of being involutive, that… May 12, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer Did another simple test, and actually the method does behave… May 12, 2026 at 5:03 pm

Related Questions

I am doing regression testing using NUnit, WatiN and VB.net. What i am doing
We've just separated our SQL Server database adapters to allow for differences between SQL
I want to use some NSAssert stuff and other things to enable better debugging
Its been a while I have ready Mcconnell's Code Complete . Now I read

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.