I wrote code
void SEHtest(int i) {
int s = 0;
__try {
cout << "code1" << endl;
int j = 1 / s;
cout << "code2" << endl;
} __except((s = 1, i)) {
cout << "code3" << endl;
}
cout << "code4" << endl;
return;
}
int main() {
SEHtest(-1);
return 0;
}
and i'm waiting for output
code1
code2
code4
but i have only
code1
and infinite loop.
Why is it?
adding volatile
keyname to s and j didn't fix it.
The infinite loop is caused because the exception is rethrown every time you resume execution. It doesn't matter that you set the value of
s = 1
in the filter because the execution is resumed from the instruction that caused the trap, which in this case is the divide by zero. If you reorganize the code as follows you'll see that the exception is continually being thrown:The result should read:
The exception continues to be thrown because execution is resumed on the instruction that divides by zero, not on the instruction that loads the value of s. The steps are:
I don't think you'll be able to resume from that exception.