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

13th Feb 2025, 7:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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.
14th Feb 2025, 3:17 AM
Afnan Irtesum Chowdhury
Afnan Irtesum Chowdhury - avatar
0
Thanks
14th Feb 2025, 3:48 AM
Ketan Lalcheta
Ketan Lalcheta - avatar