Consider the following code:
try {
int *i = NULL;
i[100] = 20;
catch (...) {
std::cout << "Exception Caught";
}
When running this code, it crashes (obviously, accessing a NULL pointer).
Although, in Debug mode, Visual Studio states about an Uncaught exception, regarding write access violation.. also understandable.
I expected an exception to be caught here, but none is.
My conclusion is that no exception is being thrown.
So why is VS alerting about an uncaught exception ?
This question all started when I wanted to protect myself from code by another programmer, and wanted to wrap the calls to his functions with try-catch, assuming that he might be doing some access violations. But if I can only catch exceptions that are expicitily thrown, I’m pretty screwed.
The only other explanation I may have is that this is because of some kind of Project or compiler configuration.
I ran this in a new C++ Console Application is VS2005.
Thanks
Access violation is not C++ exception and cannot be caught by catch operator. Unhandled exception message in the Output window doesn’t mean that this is C++ exception. First-chance and unhandled exception messages are generated both for C++ exceptions and any other exceptions like access violation. Non-C++ exceptions can be caught by __try – __except block.