Search code examples
linuxvisual-c++g++porting

Alternative for std::exception(const char*) non-standard constructor


My Visual C++ code uses the std::exception constructor that accepts a string and I'm trying to port the code to Linux / G++. What exception class should I use?


Solution

  • Microsoft Visual C++'s std::exception(const char*) constructor is non-standard. While in the C++ Standard Library, std::exception has a const char* what() const method, it provides no way of specifying a string except by overriding.

    You should rewrite your code to use std::runtime_error or one of the other classes from <stdexcept> as an alternative. Existing code that catches std::exception does not need to be changed, of course, since std::runtime_error derives from it.