Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. // NOTE: the value of the C/C++, Code Generation, Enable C++ Exceptions setting in important
  5. // Try it both with /EHsc (the default) and /EHa to see the difference
  6.  
  7. class Example {
  8. public:
  9.     ~Example() { std::cout << "destructed" << std::endl; }
  10. };
  11.  
  12. int filterException(int code, PEXCEPTION_POINTERS ex) {
  13.     std::cout << "Filtering " << std::hex << code << std::endl;
  14.     return EXCEPTION_EXECUTE_HANDLER;
  15. }
  16.  
  17. void testProcessorFault() {
  18.     Example e;
  19.     int* p = new int;
  20.     delete p;
  21.     *p = 42;
  22. }
  23.  
  24. void testCppException() {
  25.     Example e;
  26.     throw 42;
  27. }
  28.  
  29. int main()
  30. {
  31.     __try {
  32.         testProcessorFault();
  33.     }
  34.     __except (filterException(GetExceptionCode(), GetExceptionInformation())) {
  35.         std::cout << "caught" << std::endl;
  36.     }
  37.     __try {
  38.         testCppException();
  39.     }
  40.     __except (filterException(GetExceptionCode(), GetExceptionInformation())) {
  41.         std::cout << "caught" << std::endl;
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement