2

I was trying sample program to understand how std::terminate feature works - but on VC++ 2019 I always get an un-handled exception error:

// set_terminate example

#include <iostream>       // std::cerr
#include <exception>      // std::set_terminate
#include <cstdlib>        // std::abort

    void myterminate () {
      std::cerr << "terminate handler called\n";
      abort();  // forces abnormal termination
    }
    
    int main (void) {
      std::set_terminate (myterminate);
      throw 0;  // unhandled exception: calls terminate handler
      return 0;
    }

I always get the below error in VC++ -

Unhandled exception at 0x761340B2 in Abandon.exe: Microsoft C++ exception: int at memory location 0x0097FAE8

Whereas the expected output is -

terminate handler called
Aborted

What wrong am I doing?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • does this code compiles? coz abort() belongs to std:: – ΦXocę 웃 Пepeúpa ツ Sep 06 '20 at 15:00
  • It compiles fine in VC++ 2019 – Programmer Sep 06 '20 at 15:02
  • 1
    Run it outside the debugger. ctrl-F5 – Retired Ninja Sep 06 '20 at 15:10
  • Running outside debugger mode does produces the expected result I see the handler getting called followed by abort message window - can you please let me know what is ctrl + F5 (running outside debugger mode) does? – Programmer Sep 06 '20 at 15:22
  • 1
    It runs the program without the debugger attached. Same thing if you double click it in explorer. – Retired Ninja Sep 06 '20 at 15:29
  • @Programmer -- concerning `abort()` versus `std::abort()`, when a source file uses `#include `, all of the names in that header are defined inside `std`; they also **might** be defined in the global namespace. And conversely, when a source file uses `#include `, all of the names in that header are defined in the global namespace, and they also **might** be defined inside `std`. Portable code uses either `#include ` and `std::abort()` or `#include ` and `abort()`. – Pete Becker Sep 06 '20 at 17:49

0 Answers0