Why is the interrupt method called?

The following abort program calls the method, even if I have an applicable catch statement. What is the reason?

 #include <iostream> #include <string> using namespace std; int main() { try { cout << "inside try\n"; throw "Text"; } catch (string x) { cout << "in catch" << x << endl; } cout << "Done with try-catch\n"; } 

When I run the program, I get only the first inside try , and then I get this error:

enter image description here

Why is the abort call called even when I am handling a string exception?

+6
source share
4 answers

Pretty simple!

You threw char const* , but there is no corresponding catch for it.

Did you mean throw std::string("..."); ?

+14
source

Yes, you need to catch char const *, not std :: string!

+1
source

Besides what the other answers say, as a general tip: Throw what is deduced from std::exception , and if nothing else, in your top handler, don't catch std::exception& or const std::exception& . It will, for example, have avoided this situation. See also

+1
source

change the type to char* and it works as expected.

0
source

Source: https://habr.com/ru/post/891819/


All Articles