fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <cstdlib>
  4. #include <stdexcept>
  5.  
  6. int news = 0;
  7. int deletes = 0;
  8.  
  9. void* operator new ( std::size_t count ){
  10. news++;
  11. void* p = malloc(count);
  12. if(nullptr == p){
  13. throw std::bad_alloc();
  14. }
  15. return p;
  16. }
  17.  
  18. void operator delete ( void* ptr ){
  19. deletes ++;
  20. if(ptr != nullptr)
  21. free(ptr);
  22. }
  23.  
  24. class Bomb{
  25. public:
  26. ~Bomb(){
  27. throw std::runtime_error("Thrown in destructor");
  28. }
  29. };
  30.  
  31. int main(){
  32.  
  33. try{
  34. delete (new Bomb());
  35. }catch(...){
  36. std::cout<<"Caught exception"<<std::endl;
  37. }
  38. int n = news;
  39. int d = deletes;
  40.  
  41. std::cout<<"News: " <<n<<" Deletes: "<<d<<std::endl;
  42.  
  43. return 0;
  44. }
  45.  
Runtime error #stdin #stdout #stderr 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::runtime_error'
  what():  Thrown in destructor