I am reading about Structured Exception Handling in C. Here is an example code which does not work as expected:
This code is taken from here:
http://msdn.microsoft.com/en-us/library/ha52ak6a.aspx
// C4733.cpp
// compile with: /W1 /c
// processor: x86
#include "stdlib.h"
#include "stdio.h"
void my_handler()
{
printf("Hello from my_handler\n");
exit(1);
}
int main()
{
_asm {
push my_handler
mov eax, DWORD PTR fs:0
push eax
mov DWORD PTR fs:0, esp // C4733
}
*(int*)0 = 0;
}
This code should print the message, "Hello from my_handler" when the exception is triggered by trying to write to an invalid memory address. However, it appears that the exception handler is not called at all.
I compiled this code and tried debugging it with Olly Debugger. When the exception occurs, I try passing the exception to the application defined exception handler (by pressing, Shift + F9) but it does not get called. I set a breakpoint at the exception handler (first instruction), but it never reaches that section of code.
What might be the reason for this?
/ZI /MTd
compiler options ? In this way, I have got the expected result. – Zoezoeller