Std :: exception what () returns "std :: exception"

Java man is stuck here executing some C ++. I will catch the exception and try to diagnose where it comes from (unfortunately, the exception does not occur when launched via gdb). However, when I print out what () exceptions, I just get the string "std :: exception". Is it specific to anything in the standard library, or does it make many standard exceptions? Here is what I do for printing:

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

Output:

 std::exception 

In addition, I work on a rather large code base, it is possible that this comes from some kind of exception at our end, but I still have to find it using the usual search methods, so now I'm leaning towards this from the standard libraries.

I am using g ++ 4.8 if relevant.

+7
c ++ exception stl
source share
2 answers

C ++ exceptions are completely different from Java exceptions.

The C ++ standard states that the string returned by function () is completely arbitrary and is determined by the implementation:

 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, "std :: exception" is perfectly consistent with the C ++ standard.

Do not rely on C ++ exceptions to pinpoint where they were thrown from after you catch them, such as Java. This is beyond the scope of the C ++ standard. In C ++, an exception is nothing more than a mechanism for passing the execution control flow.

Having said that: many C ++ implementations will provide you with some implementation-specific mechanisms for dumping the current backtrace stack, at best from the runtime library. See the C ++ compiler documentation for more information.

gcc, for example, offers backtrace () along with some built-in gcc functions to convert raw addresses returned by backtrace () to characters, and other functions to demonstrate characters. Using this, you can come up with a crude counterpart to Java exception handling; although the gcc implementation is not perfect and has some functional holes, it also requires prior planning and custom exception classes, whose constructors fix the current stack stack (before the exception is actually thrown); and, once caught, an instance of the excluded exception class can be checked for captured return information.

But that doesn’t really help your current situation. I would suggest that you check your C ++ compiler documentation, as I suggested, and also explore the capabilities of your debugger. The C ++ debugger should allow you to set a breakpoint when all exceptions are thrown, and before they are caught so that you can check the stack backtrack through the debugger when an exception occurs.

+3
source share

This may be due to two things:

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

  • The class derived from std::exception is copied to std::exception . This is a problem called Object Slicing .

I just made a second mistake actually. I had this code:

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

You must do std::exception& e . I know that you did not make this mistake, but it is possible that someone else made the code inside (or someone from Google).

+10
source share

All Articles