Jump to content
  • Advertisement
Sign in to follow this  
Enerjak

Little help with throwing exceptions (custom ones).

This topic is 2445 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, now, I had a generic class called IrrOrderedLinkedList<T> ok? now, if i make it a string list, nothing happens and it works fine
BUT when I make it an int, i get an error because of these two lines:


string s = " ";
s += "Item " + current->data + " is not in the list.";
throw s;
throw "The item " + current->data + " is not in the list";


not sure how to solve this problem, hopefully, someone in the audience can help out.

Share this post


Link to post
Share on other sites
Advertisement
You need to convert the int (current->data) to string.
This is C++, you can't just join an integer to a std::string.


char buffer[128];
sprintf(buffer, "%d", current->data);
s += "Item " + buffer + " is not in the list.";

Share this post


Link to post
Share on other sites

This is C++, you can't just join an integer to a std::string.
char buffer[128];
sprintf(buffer, "%d", current->data);
s += "Item " + buffer + " is not in the list.";

That is fairly efficient (although error checking would be nice), but a complete hassle if you want to expand it to other types beyond just integers.

In C++, we have stringstreams for this purpose:
std::ostringstream os;
os << current->data;
s += os.str();


Or, if you have access to Boost, see lexical_cast.

Share this post


Link to post
Share on other sites
You should be thowing exception objects, not strings. The standard exception classes can store strings:

#include <stdexcept>

if ( /* not found */ )
{
std::stringstream message;
message << "Item " << current->data << " is not in the list.";
throw std::runtime_error(message.str());
}

I'm not sure that failing to find an element in a list is even an exceptional case (depending on the nature of the function). Returning a boolean value, or otherwise avoiding exceptions, might be a better design. Note for instance that there are relatively few cases in the C++ standard library where exceptions are thrown (allocation failures aside).

Share this post


Link to post
Share on other sites
The problem is that current->data’s type matches the T template parameter.

Resolve the types:

[color=#008800]"Item "[color=#000000] [color=#666600]+[color=#000000] current[color=#666600]->[color=#000000]data [color=#666600]+[color=#000000] [color=#008800]" is not in the list."[color=#666600];
=

[color=#008800]"Item "[color=#000000] [color=#666600]+[color=#000000] std::string[color=#000000] [color=#666600]+[color=#000000] [color=#008800]" is not in the list."[color=#666600];

And once you used an int for the T value, it became:

[color=#008800]"Item "[color=#000000] [color=#666600]+[color=#000000] int[color=#000000] [color=#666600]+[color=#000000] [color=#008800]" is not in the list."[color=#666600];

There is no operator for the int-to-std::string conversion in the std::string class.

Using << as suggested by rip-off will fix this issue, and following the rest of his advice won’t hurt.


L. Spiro

Share this post


Link to post
Share on other sites
Worse still, if you had written "Item is not found in list: " + current->data, it would be interpreted as pointer arithmetic, and could result in undefined behaviour depending on the value of current->data.

Share this post


Link to post
Share on other sites
If you are referring to my latest post, the answer is that the string literal is an array of characters, which decays to a pointer. Adding an integer to a pointer is pointer arithmetic, which advances the pointer N elements, where N is current->data. This is all legal, just unintended. It becomes illegal if the value of current->data is negative, or exceeds the length of the string.

While it is legal to take a pointer to the "one past the end" of this array (which in this case would point just after the implied NUL character in the string literal) the chances are that whatever is done next with this pointer will be illegal. Passing it to a C style string API, such as operator<<(std::ostream &, const char *) is not allowed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!