Reputation: 147
I can't find a way to actually enable c++ exception handling in visual studio 2017. Maybe I am missing something trivial. I've searched a lot and found nothing that solves this simple issue.
Even this simple code does not act as expected:
#include <iostream>
//#include <exception>
int main(int argc, char *argv[])
{
try
{
int j = 0;
int i = 5 / j;
std::cout << "i value = " << i << "\n";
std::cout << "this line was actually reached\n";
}
catch (...)
//catch (std::exception e)
{
std::cout << "Exception caught!\n";
return -1;
}
std::cout << "Exception was NOT caught!\n";
std::cin.get();
}
When I execute this simple code the program crashes, like the exception is never caught, I tried also with std::exception variant, and i get the same result.
The division by zero is just an example, I could have wrote also something like this:
ExampleClass* pClassPointer = null;
pClassPointer->doSomething();
and expected that a nullpointer exception was automatically thrown and captured by the catch(...) clause.
Just as an information, I added the line:
std::cout << "i value = " << i << "\n";"
otherwise the compiler optimization would have skipped most of the code and the result (in RELEASE mode) was:
this line was actually reached
Exception was NOT caught!
In the properties page of the project, in the option "Enable C++ Exceptions" I have the value: "Yes (/EHsc)", so I think exceptions should be actually enabled.
Am I missing something? Thank you in advance. Andrea
---- Edit
I actually just found out that changing the option "Enable C++ Exceptions" with the value "Yes with SEH Exceptions (/EHa)" instead of "Yes (/EHsc)" allows to capture access violation and divide by zero errors in the catch(...) clause. I've also found very useful information about this subject and whether or not is a good idea to catch those type of exceptions in the replies to this other thread: Catching access violation exceptions?
Upvotes: 0
Views: 1867
Reputation: 76498
In C++ you throw
an exception, and, having done that, you can catch
it. If you didn't throw it, you can't catch it.
Confusingly, there are many kinds of errors that are generically referred to as "exceptions". This is especially true in floating-point math, where anything that goes wrong is a "floating-point exception". It's the same word, but those exceptions are not C++ exceptions, and you cannot reliably catch them.
In general, when something goes wrong in your program you get undefined behavior, that is, the language definition does not tell you what the program does. So you're on your own: if your compiler documents what it does when a program divides an integer value by 0, then you can rely on that behavior. If not, don't count on anything.
Upvotes: 4
Reputation: 188
#include <iostream>
int main(int argc, char *argv[])
{
try
{
int j = 0;
int i ;
if(j==0)
throw j; // you have throw some variable or atleast use throw for your
//catch to work
std::cout << "i value = " << j/5<< "\n";
std::cout << "this line was actually reached\n";
}
catch (int e) /*catch will work only if the data type of argument matches with the thrown variable's data type if it does not then the compiler's default catch will run*/
{
std::cout << "Exception caught!\n";
return -1;
}
std::cout << "Exception was NOT caught!\n";
std::cin.get();
}
if u still have any questions ask in comments!
Upvotes: 0