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_throw n = false;
try
{
hash_table.inse rt(some_value);
}
catch(const std::runtime_er ror& e)
{
std::cerr << e.what() << std::endl;
exception_throw n = true;
}
if(!exception_t hrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}
Using the flag variable exception_throw n 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_throw n = false;
try
{
hash_table.inse rt(some_value);
}
catch(const std::runtime_er ror& e)
{
std::cerr << e.what() << std::endl;
exception_throw n = true;
}
if(!exception_t hrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}
Using the flag variable exception_throw n 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
Comment