Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::exception's what() returns "std::exception"

Tags:

c++

exception

stl

Java person here stuck doing some c++. I'm catching an exception and trying to diagnose where it is coming from (regrettably the exception is not thrown when run via gdb). However, when I print out the what() of the exception I simply get the string "std::exception". Is this specific to anything in the standard library or do lots of standard exceptions return this? Here is what I am doing to print:

    } catch (const std::exception & ex) {
      std::cout << ex.what() << std::endl;
    }

The output is just:

std::exception

Also, I'm working in a fairly large code-base, it is possible that this is coming from some exception on our end but I have yet to find it through regular search techniques so I'm currently leaning towards this coming from the standard libraries.

I'm using g++ 4.8 if that is relevant.

like image 330
Pace Avatar asked Feb 17 '15 02:02

Pace


People also ask

What is std :: Runtime_error?

std::runtime_errorDefines a type of object to be thrown as exception. It reports errors that are due to events beyond the scope of the program and can not be easily predicted.


2 Answers

This could be because of two things:

  1. Someone just does throw std::exception() somewhere, which is not very helpful.

  2. A class derived from std::exception gets copied to std::exception. This is a problem called Object Slicing.

I just made the second mistake myself actually. I had this code:

try
{
    // Some Boost stuff
}
catch (std::exception e)
{
    cerr << e.what() << endl;
}

You must make sure to do std::exception& e. I know you didn't make this mistake but it's possible someone else did further within the code (or someone coming here from Google).

like image 123
Timmmm Avatar answered Oct 18 '22 01:10

Timmmm


C++ exceptions are completely different from Java exceptions.

The C++ standard specifies that the string returned by what() is completely arbitrary, and implementation-defined:

virtual const char* what() const noexcept;

Returns: An implementation-defined ntbs.
Remarks: The message may be a null-terminated multibyte string
(17.5.2.1.4.2), suitable for conversion
and display as a wstring (21.3, 22.4.1.4). The return value remains 
valid until the exception object
from which it is obtained is destroyed or a non-const member
function of the exception object is called.

The return value you're getting, "std::exception" is perfectly compliant with the C++ standard.

Don't rely on C++ exceptions to tell you exactly where they were thrown from after you catch them, like Java does. This is outside of the scope of the C++ standard. In C++, an exception is really nothing more than a mechanism for transferring execution control flow.

Having said that: many C++ implementation will provide you with some implementation-specific mechanisms for dumping the current stack backtrace, to the best of the runtime library's abilities. Check your C++ compiler's documentation for more information.

gcc, for example, offers backtrace(), together with some gcc internal functions to convert the raw addresses returned by backtrace() into symbols, and other functions for demangling the symbols. Using that, a rough analogue to Java's exception handling can be concocted; although gcc's implementation isn't perfect, and has some functional holes, and that also requires advance planning, and custom exception classes whose constructors capture the current stack frame (before the exception gets actually thrown); and, once caught, the thrown exception class instance can be inspected for the captured backtrace information.

But that doesn't really help your current situation. I would suggest that you check your C++ compiler's documentation, as I suggested, and also investigate your debugger's capabilities. A C++ debugger should allow you to set a breakpoint when any exceptions get thrown, and before it is caught, so that you can examine the stack backtrace via the debugger, when the exception occurs.

like image 40
Sam Varshavchik Avatar answered Oct 18 '22 01:10

Sam Varshavchik