Chiel
Chiel

Reputation: 119

Visual Studio Exception settings ignored C++

I'm testing the assert method in C++ in Visual Studio 2017 and am getting an assertion exception as I would have expected. But after turning off all(!) exception settings I still get an exception thrown before it can be handled by my catch block (see below for an example).

try {
    assert(validate(1363821) == false);
    assert(validate(3848238) == true);
    printf("Validation correctly implemented.");
} catch ( exception & e ){
    const string error = e.what();
    printf("Validation failed!");
}

So my questions are:

My exception settings are not set as is shown below: enter image description here

Any help is greatly appreciated!

Upvotes: 0

Views: 686

Answers (1)

user7860670
user7860670

Reputation: 37607

Assertion failure is not supposed to throw any exceptions. Instead it performs some implementation-specific report actions (such as printing an error message into stderr or showing that dialog) and then calls std::abort. So catch block and / or exception handling settings in IDE won't do anything in this situation. If you want assertion to throw an exception then you will need to write your own assert macro substitution.

If you are looking for some sort verification checking then you better utilize some dedicated framework, such as boost::test. Then you can write simply:

 BOOST_AUTO_TEST_CASE(Doc_Parse_Empty)
 {
     BOOST_TEST(validate(1363821) == false);
     BOOST_TEST(validate(3848238) == true);
 }

It will also handle success/failure reporting automatically and seamlessly integrates into VS.

Upvotes: 2

Related Questions