Advertisement
andruhovski

SP-0301b

Feb 15th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // exception_set_terminate.cpp
  2. // compile with: /EHsc
  3. //http://www.argc-argv.com/5_****/article01.pdf
  4. #include "stdafx.h"
  5. #include <exception>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. void termfunction()
  11. {
  12.     cout << "My terminate function called." << endl;
  13.     abort();
  14.     exit(EXIT_FAILURE);
  15. }
  16.  
  17. void uefunction()
  18. {
  19.     cout << "My unhandled exception function called." << endl;
  20.     terminate(); // this is what unexpected() calls by default
  21. }
  22.  
  23. void func(){
  24.     int x = 1;
  25.     throw bad_alloc();
  26.     cout << x / (++x - 2) << endl;
  27.     //terminate();
  28. }
  29.  
  30. int main()
  31. {
  32.     terminate_handler  oldHandler1 = set_terminate(termfunction);
  33.     unexpected_handler oldHandler2 = set_unexpected(uefunction);
  34.  
  35.     // Throwing an unhandled exception would also terminate the program
  36.     // or we could explicitly call terminate();
  37.     func();
  38.     return 1;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement