Chris Frey wrote:
> In the interests of code correctness and safety, here is a patch for a new
> exception: BadFieldName.
Accepted, with modifications detailed below.
> + std::vector<std::string> known_fields;
Elided. First, exceptions are frequently copied on throw, so you don't
want exception objects to be unnecessarily large; one can get that list
another way. Second, it isn't the purpose of an exception object to
help the C++ program recover, but simply to flag an error, which the
programmer/user must cope with.
> +//: Exception thrown when lookup_by_name can't find the specified field.
> +class BadFieldName {
> +public:
> + std::string bad_field;
> + std::vector<std::string> known_fields;
> +
> + BadFieldName(const char *bad, const std::vector<std::string> &names)
> + : bad_field(bad), known_fields(names) {}
> +};
I changed this to be more like the other BadFieldName class, and like
the other exceptions: its member is called 'error' now, and it holds a
constructed error message, not just the field name.
> const ColData Row::lookup_by_name(const char* i) const
> {
> - return (*this)[res->field_num(std::string(i))];
> + int si = res->field_num(std::string(i));
> + if( si >= res->num_fields() )
> + throw BadFieldName(i, res->field_names());
> + return (*this)[si];
I rearranged this code so that the most common path is first, and the
exception is in the 'else' branch.