Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » how to input an int into std::string? and an exception question
icon5.gif  how to input an int into std::string? and an exception question [message #13362] Thu, 03 January 2008 04:10 Go to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
iota is good, but that is for char *.
i used std::stringstream, which accepts int well, but when i convered it into std::string and threw it, my program crashed.
std::stringstream msg;
msg << "failed to bind to " << localSocket->ip() << ":" << localSocket->port()/* this is an int */;
throw std::string(msg.str()); //or simply  throw msg.str();

...
catch (std::string err) ...

& i want standard C++ method, not String or alike.
another question is why my functions cannot throw an exception by default
child() {... throw std::string("error"); }
  dad() {... child(); ...}/*crashes*/
  mum() {... try { child(); }/*this works*/
    catch (std::string err) { throw err; }}
grand() {... try { dad()/* or mum()*/; }
  catch (std::string err) {....}}

today i just ran mum() again, and it crashed, too... (since i had been Debuggin, i could locate the last sentence is throw)

[Updated on: Thu, 17 January 2008 07:05]

Report message to a moderator

Re: how to input an int into std::string? [message #13368 is a reply to message #13362] Thu, 03 January 2008 13:18 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
When you throw an exception, if you have no way to catch that exception, it will crash your program.

That's actually kind of the point behind exceptions.

If you never want your program to crash due to an exception, but want it to die gracefully, you have to provide some way to handle all exceptions. Sometimes, this is accomplished in main with something like:

int main( int argc, char** argv )
{
  try
  {
     millions_of_functions();
     even_more_functions();
     maybe_yet_more_functions();
  }
  catch( ... )
  {
     // Some error handling routine here... like maybe:
     std::cerr << "Something catastrophic happened." << std::endl;
  }
}


So, based on what I'm reading in your message, I do not see the nature of your problem. It looks to me that your program should be crashing, since you're asking it to do so.

Generally, the folks experienced with C++ that I've talked to seem to agree that exceptions should be used in very rare cases, and ought to be avoided if possible. You might only use exceptions for truly exceptional situations, where you can see that the system will have a serious problem that destabilizes memory or registers, and want to clearly indicate where the problem occurred so you can fix it more easily.

If you can, you should try to indicate most error conditions through other means, perhaps through an error state, return code, or something you change in a parameter.
Re: how to input an int into std::string? [message #13370 is a reply to message #13368] Thu, 03 January 2008 14:39 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
bonami: the mum should crash too, as you do another throw in the catch clause, which is not catch anywhere.

tvanriper wrote on Thu, 03 January 2008 13:18

Generally, the folks experienced with C++ that I've talked to seem to agree that exceptions should be used in very rare cases, and ought to be avoided if possible. You might only use exceptions for truly exceptional situations, where you can see that the system will have a serious problem that destabilizes memory or registers, and want to clearly indicate where the problem occurred so you can fix it more easily.

If you can, you should try to indicate most error conditions through other means, perhaps through an error state, return code, or something you change in a parameter.


I think it's about how the source code looks.
If the exception version looks shorter and cleaner and is easier to read and understand, you should use it, even if it is not serious problem state what is handled there.
If the "if" version with error return codes looks good enough, the exception version would likely look more complex.

In case of performance critical code you should benchmark final version with profiler, and rewrite only the most critical code parts, with proper comments why you are using more ugly (but faster) code in that space and how it improves performance.

And you should try to create some policy for your project where/why to use exceptions, so they are used in similar context trough whole source (my biggest problem with them, often two different components from me are written in different way, one does use exceptions a lot for error states, other does use if/return extensively, especially if rewrite with different error handling would lead to similar source code and there's no clear winner).

But I don't think there's any major reason to avoid exceptions for all costs. Especially if they will make the source code easier to understand and easier to maintain/change later.
Re: how to input an int into std::string? [message #13383 is a reply to message #13362] Thu, 03 January 2008 18:58 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
Yeah, ultimately, it's about whatever reads the best.

I probably spoke mostly to folks who find it less legible to have the execution of the code shift dramatically due to an exception.

But, ultimately, it's about whatever makes sense for your particular context.
Re: how to input an int into std::string? [message #13405 is a reply to message #13370] Fri, 04 January 2008 03:46 Go to previous message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
Mr.Ped,
mum() is caught in grand() as below,
grand() { ... try { mum(); } catch (std::string err) {...}}
that is why it should not crash.
and i suppose dad() should not crash either, because, though dad() does not catch the exception of child(), it should throw the exception by default, which will be caught in grand().
Previous Topic: Proposed change for TabCtrl: Color of text...
Next Topic: How can Layout be used for non-TopWindow special widget?
Goto Forum:
  


Current Time: Thu Mar 28 23:42:38 CET 2024

Total time taken to generate the page: 0.01221 seconds