Reputation: 2845
I throw this error in my C++ program and don't handle it:
throw std::runtime_error("X failed because " + my_string);
I compile and run it with Visual Studio 2013, and get the following error:
Unhandled exception at 0x7617C42D in bla.exe: Microsoft C++ exception: std::runtime_error at memory location 0x009FEA98.
How can I see the message "X failed because ..." without handling the error in the code?
Upvotes: 10
Views: 3598
Reputation: 10039
You could easily create an exception wrapper, that outputs the exception's what()
to the debug output. While this would not require you to add catch blocks to handle the immediate location that your exceptions are thrown, it would require you to wrap all your throw calls with the wrapper class. For example:
class ExceptionOutputDebug : public std::exception
{
public:
ExceptionOutputDebug(const std::exception& e)
{
OutputDebugString(e.what());
OutputDebugString("\n");
if (1) // avoids C4702 (unreachable code)
throw e;
}
};
Usage:
throw ExceptionOutputDebug(std::runtime_error("Die"));
Will then output to the debug output window:
Die
First-chance exception at 0x76CAC42D in blah.exe: Microsoft C++ exception: std::exception at memory location 0x019EFEBC.
This might be useful if your program is throwing a lot of exceptions, and have disabled the debugger breaking on them. It could also be useful if running outside the debugger, and OutputDebugString
calls were replaced with some external logging mechanism (could be as simple as printf
for console applications).
Upvotes: 1
Reputation: 3056
If you are just trying to figure out which exception caused your program to terminate while debugging, you can simply Break then navigate the call stack to see where the exception is thrown.
If you are in the Debug mode, the top of the call stack will probably point at _CxxThrowException
. In that case, you could inspect its argument pExceptionObject
and even add something like ((std::exception*)pExceptionObject)->what()
to the watch list. However, this relies on a few assumptions that are not always valid.
It is really easier to add a catch (std::exception& e)
and inspect the error there.
Upvotes: 1