#include <stdexcept> int main() { throw std::runtime_error("Error"); } 

Why DevC++ this program in DevC++ output the Error to the console, but not in Visual Studio 2015 ? What parameter can need to be changed?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

Because different implementations of the class runtime_error.

If you need the error to be output to the console, format the error like this:

std::cerr << std::runtime_error("Error").what() << std::endl; . And don't forget to include the header: #include <iostream>

You can also extend std::exception and write your output to the console:

 class customError: public std::exception { public: customError(const std::exception& e) { OutputDebugString(e.what()); OutputDebugString("\n"); if (1) // avoids C4702 (unreachable code) throw e; } }; 

Using:

throw customError(std::runtime_error("Error"));

    Let's look at the code again:

     int main() { throw std::runtime_error("Error"); } 

    So you have an uncaught exception. That nobody intercepts or processes at all. Who do you think should get a message about him?

    The medium can intercept and display a message (which no one requires), but is absolutely not obliged to do this.

    The best thing you can do — if you don't catch the exception — is to set your own terminating function, which is called when an uncaught exception occurs, such as

     void Quit() { cout << "Uncaught exception\n"; exit(1); }; int main() { set_terminate(Quit); throw std::runtime_error("Error"); } 

      Because Visual Studio does not print the content that can be obtained from what() when it falls? What makes you think that she should do this? Apparently, DevC ++ decided that it was useful and made this functionality. In Visual Studio, this is not and, frankly, I do not see any benefit in it.