E
Eric Lilja
Hello, I'm working with a hash table that is encapsulated in a class. One of
its member functions insert() throws an exception
if the insertion fails (for example, if the value was already present in the
hash table). Now I have client code that looks like this:
bool exception_thrown = false;
try
{
hash_table.insert(some_value);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
exception_thrown = true;
}
if(!exception_thrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}
Using the flag variable exception_thrown strikes me as a bit ugly...how
should I deal with this? If I'm calling a function that
may throw an exception I want to catch that exception. I can change the hash
table class itself if I need to..maybe the insert()
member function should return some error code instead but runtime_error
seems a bit more convenient since it can (should I think)
contain a description of the error. Please advise.
/ Eric
its member functions insert() throws an exception
if the insertion fails (for example, if the value was already present in the
hash table). Now I have client code that looks like this:
bool exception_thrown = false;
try
{
hash_table.insert(some_value);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
exception_thrown = true;
}
if(!exception_thrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}
Using the flag variable exception_thrown strikes me as a bit ugly...how
should I deal with this? If I'm calling a function that
may throw an exception I want to catch that exception. I can change the hash
table class itself if I need to..maybe the insert()
member function should return some error code instead but runtime_error
seems a bit more convenient since it can (should I think)
contain a description of the error. Please advise.
/ Eric