how to catch a general exception and show its derived what()

The problem happens with code like this:

#include <cstdlib> #include <iostream> #include <stdexcept> using namespace std; int main(int argc, char** argv) { try { throw runtime_error("Message"); } catch (exception e) { cout << e.what(); } return 0; }

I expect Message to appear. But the result was std::exception. I thought the subclass virtual functions can be called from the superclass reference. How can fix that?


C++ makes an explicit distinction between reference and value copy. Use

catch (const std::exception& e)

to catch by reference instead of value.


  • How to call DeviceIoControl to retrieve the amount of memory it needs?
  • Returning a reference from a function in c++
  • Why wont my program execute the second catch block, when the error exists?
  • How can I open a file for reading & writing, creating it if it does not exist, without truncatin
  • Pass Arbitrary Sized 2 Dimension Array
  • What is lua_len() alternative in Lua 5.1?
  • std::system Exception when instantiating a singleton object
  • Call a macro with parameters : Python win32com API
  • Get the App path without the app name at the end of the app
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Building Qt project for C++11 standard
  • Getting short path in python
  • C++ friend class std::vector
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Cloud Code function running twice
  • How to read piped content in C?
  • C: Incompatible pointer type initializing
  • Thread safety of a fluent like class using clone() and non final fields
  • Converting a WriteableBitmap image ToArray in UWP
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Accessing IRQ description array within a module and displaying action names
  • AES padding and writing the ciphertext to a disk file
  • output of program is not same as passed argument
  • Convert array of 8 bytes to signed long in C++
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Akka Routing: Reply's send to router ends up as dead letters
  • Linker errors when using intrinsic function via function pointer
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal