Reputation: 17
I'm using MinGW gcc (or g++) 7.1.0 on Windows 10.
Normally, throwing an std::runtime_error
shows information like this:
terminate called after throwing an instance of 'std::runtime_error'
what(): MESSAGE
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
But the following code only shows the last two lines, and the what()
information is lost:
#include <stdexcept>
using namespace std;
int main() {
try {
throw runtime_error("MESSAGE");
} catch (...) {
throw;
}
}
So the code above only outputs:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
The same thing happens if I replace ...
with const exception&
, const runtime_error&
(or without const
, without &
, or without both).
As I know, throw;
rethrows the current caught exception. So why isn't what()
shown?
Upvotes: 0
Views: 69
Reputation: 37600
What makes you think that rethrowing exception discards the information given by 'what()'? You never inspect what what()
returns after rethrowing. This application has requested...
message is shown because you uncaught exception caused program to be terminated. what()
content is not supposed to be printed automatically.
You can print value return by what()
without any problem:
#include <stdexcept>
#include <iostream>
int main()
{
try
{
try
{
throw ::std::runtime_error("MESSAGE");
}
catch (...)
{
throw;
}
}
catch(::std::exception const & exception)
{
::std::cout << exception.what() << ::std::endl;
}
}
Upvotes: 1