views:

36

answers:

2

Assuming the stream extraction won't fail, will this

if( !(stream >> token) )
    throw runtime_error( "Unexpected end of recipe." );
else if( token == "something" )
    // do something
else
    throw runtime_error( "Unknown token" );

Work like this

if( !(stream >> token) )
    throw std::runtime_error( "Unexpected end of recipe." );

if( token == "something" )
    // do something
else
    throw std::runtime_error( "Unknown token" );

(stream = std::stringstream and token = std::string) And the expected result being token contains the next ... token... from the stream? Is there a better way to handle errors? Like cerr << "error whatever" << endl; exit(); or something, or is my std::runtime_error good enough?

+3  A: 

Yes, the logic is exactly the same.

And I wouldn't ever call exit() - at least in your code the exception can be caught by some other code instead of just killing the app. You never know if you will use this code inside a larger application sometime in the future so it's better to keep your options open ;)

deanWombourne
+1  A: 
daramarak