0
Why exception is not transferred
Hi I am trying to rethrow exception which was caught from thread. Why output is as below: In main thread : std::exception In main thread : std::exception My exception was set as "Issue in thread" and it is missing in output. https://sololearn.com/compiler-playground/cSfSYmu6Y64H/?ref=app https://sololearn.com/compiler-playground/cV10cmY1mfC8/?ref=app
2 Answers
+ 2
The problem is that std::exception is an abstract base class, and invalid_argument (which is derived from std::exception) should be used directly, not as a base class.
The main problem lies in how you're throwing the exception object. The type of ex here is std::exception, which doesn't store the specific type information of the thrown exception (like std::invalid_argument), leading to the output being "std::exception" rather than the message you set in the invalid_argument object.
As std::exception::what() has a generic message which is "std::exception"(which is what was showing up in your console in your code) and it doesn't store custom messages, you need to use std::invalid_argument::what() to display the "Issue in main thread" message.
Replace the line 12 and 13 of your code with this:
throw std::invalid_argument("Issue in thread");
This should work correctly and throw your custom exception message.
0
Thanks