ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You have a dangling pointer. c_str is returning a pointer to the buffer in a temporary object - the return value of str().
If you changed ostringstream to a std::string and assigned to it you'd find the c_str() points to valid memory.
I cannot understand the rationale behind combining the answer of both of you.
ex is declared only inside the try-block, then the content is destroy after reaching the catch-block. In that case neither ostringstream nor string is valid.
I have also tried catch(std::exception e) but it's not helping. ex is not copied to e.
I cannot find any satisfactory example of throwing an object derived from std::exception on the Internet.
Anyone knows the right way of doing it? and perhaps explain why std::string works but std:stringstream won't.
I cannot understand the rationale behind combining the answer of both of you.
ex is declared only inside the try-block, then the content is destroy after reaching the catch-block. In that case neither ostringstream nor string is valid.
I have also tried catch(std::exception e) but it's not helping. ex is not copied to e.
The exception is copied to the catch clause. Try it and see:
Same. 'Attempt to reference a deleted function'. And it doesn't make sense to declare a global variable.
I have not delt with exceptions other than the default catching them.
Yet, having been investigating this issue I do not think OP is being written properly at all. trying to pass in a string into the exception via a data type declared within the class, then return it in that function that takes in no pharms even though it has a declared data type but no real means to return that in the function itself.
that example itself I posted in that link it does not do this, try to take an assignment then return it.
Where that exception return value within the exception function is suppose to be the error itself as defined by the programmer, to be return in that exception class function itself and not taking in from elsewhere so that it maybe over written by another means.
that sounds logical to me .
that is not to say one cannot over ride another exception catch to return the programmers error message rather then the "build in" one for an external means.
The dynamic content I try passing from the try-block to the catch-block is perhaps another info from an given API, or __FILE__,__LINE__ values. It could take several forms depending on the situations. That's why I try to format the what() content using the ostringstream. It should be a legitimate reason.
In the Dangle class example, I will interpret the problem as: the ss.str().c_str() returns a pointer pointing to the ss.str() instance, but which is destroyed after const char* str() {} exits.
Code:
Dangle(const char* s) {
ss << s;
std::cout << ss.str().c_str() << "\n";
std::cout << str() << "\n"; // <- This is what is wrong.
}
My apologies for misleading you. The original program was correct that the exception was still valid in the catch block and that e was a proper reference to it. I learned today that the str() returned from ostringstream is a temporary std::string.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.