The advantage is that exceptions automates the practice of bubbling up boolean returns. For example, this might be exception-less error checking code.
Code:
// Reads a person's age from the string database
// Returns 0 on error and sets errno
long getAge(char const * name) {
char const * age_str;
long age_num;
age_str = database_lookup (name);
if (errno) // Database lookup error
return 0;
if (age_str == NULL)
{ // Can't read a null value.
errno = 1;
return 0;
}
char * endptr;
age_num = strtol (age_str, &endptr, 10);
if (errno) // Range error
return 0;
if (*endptr != 0) // Didn't read the whole string, it's not an int
{
errno = 1;
return 0;
}
return age_num;
}
The hope with exceptions is that we could change that to something like this...
Code:
// Reads a person's age from the string database
// Throws on err.
long getAge(char const * name) {
char const * age_str = database_lookup (name);
if (age_str == NULL)
{ // Can't read a null value.
std::ostringstream err;
err << "DB field USERS.AGE_STR contains NULL field for USERS.NAME = " << name;
throw database_error (err.str());
}
return boost::lexical_cast<long>(age_str);
}
The second snippet handles all the same errors in all the same places, it's just all done under the hood.
The following links goe pretty into "Why exceptions are great" and how to properly use them, albeit light on examples.
http://www.parashift.com/c++-faq-lite/exceptions.html
http://www.scottcollins.net/articles/exceptions.html