Created
April 25, 2017 16:55
-
-
Save lallousx86/1ce4d9cc6b29a4730d3a90d83c6f66a4 to your computer and use it in GitHub Desktop.
try/except sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <windows.h> // for EXCEPTION_ACCESS_VIOLATION | |
#include <excpt.h> | |
int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep) { | |
puts("in filter."); | |
if (code == EXCEPTION_ACCESS_VIOLATION) { | |
puts("caught AV as expected."); | |
return EXCEPTION_EXECUTE_HANDLER; | |
} | |
else { | |
puts("didn't catch AV, unexpected."); | |
return EXCEPTION_CONTINUE_SEARCH; | |
}; | |
} | |
int main() | |
{ | |
int* p = 0x00000000; // pointer to NULL | |
puts("hello"); | |
__try{ | |
puts("in try"); | |
__try{ | |
puts("in try"); | |
*p = 13; // causes an access violation exception; | |
}__finally{ | |
puts("in finally. termination: "); | |
puts(AbnormalTermination() ? "\tabnormal" : "\tnormal"); | |
} | |
}__except(filter(GetExceptionCode(), GetExceptionInformation())){ | |
puts("in except"); | |
} | |
puts("world"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment