Catch the Exception "Fixing Access Error 0x00000000"
I am using a method from a third party DLL and it throws a "Correcting read with error 0x00000000" exception. I can't dig, so I'm just wondering if there is anyway to catch it so I don't collapse the application. I have tried the following 4 methods but none of them work.
1,
try
{
sts = resFilter->initialize(m_JPEG2000File); // it throws that exception
}
catch (...){
printf("Gotcha0...");
int a = 34;
}
2, 3 and 4
LONG WINAPI CrashHandler1(EXCEPTION_POINTERS * a/*ExceptionInfo*/)
{ std::cout << "Gotcha1!" << std::endl;
return 0;
}
void CrashHandler2()
{ std::cout << "Gotcha2!" << std::endl;}
void CrashHandler3()
{ std::cout << "Gotcha3!" << std::endl;}
// in Main()
::SetUnhandledExceptionFilter(CrashHandler1);
std::set_terminate (CrashHandler2);
std::set_unexpected( CrashHandler3 );
Test(); // It would throw "Access violation reading location 0x00000000" exception
If I debug it, an exception is thrown. If I run it at runtime, "Gotcha1!" will show up in the console, but the application will still be destroyed. Is there a way I can eat this exception?
Thanks in advance,
Ben
Edit:
@Adriano Repetti mentioned __try and __except can catch this exception.
Thanks for being heads-up guys for not eating this exception!
I have an external C # executable calling this project. I want to catch this exception, so I have a chance to log the error and not collapse the C # application. I would still terminate this very C ++ process. I am looping data in C # that starts a new C ++ process from scratch every time, so it will be a new C ++ instance. So Adriano's approach works for me.
source to share
Access violations do not "are"; thus there is severe madness.
It looks to me like you are trying to dereference a null pointer. Maybe just don't do it!
if (!resFilter) {
// do something else; e.g. did you fail to initialise the library properly?
throw "Yikes!";
}
// OK; pointer is not NULL at least: let go!
sts = resFilter->initialize(m_JPEG2000File);
Now if you have an access violation to this then the library will be extremely buggy and you should terminate it immediately unless it gets fixed or fixed.
Since you are on Windows, if all you want to do is detect a logging problem, you can use Visual Studio's non-standard __try
/ __catch
construct , but be sure to terminate the process immediately after logging the problem, because your process (especially the library state) will be volatile , and with him nothing will make sense after that!
__try {
sts = resFilter->initialize(m_JPEG2000File);
}
__except(
GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
? EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH) {
std::cerr << "OMG!\n";
exit(-1);
}
source to share
__ try and __except can catch this exception. Thanks to @Adriano Repetti!
Here is a good article about it: C ++, __try and try / catch / finally
source to share