0
$\begingroup$

Let's say that when initializing a pet class, I wanted to exclude or disallow dogs or cats as species. What would be the proper way to throw an invalid_argument exception?

using std::string;
#include <stdexcept>
using std:: invalid_argument;
struct Pet {
  const string name;
  long age = 0;
  const string species;
  Pet() : name("Dan"), age(0), species("alligator"){}
  Pet(const string & the_name, const string & the_species): name(the_name),
  species(the_species), age(0) {};
};
$\endgroup$

closed as off-topic by David Richerby, Yuval Filmus, Evil, chi, Raphael Jun 13 '17 at 4:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – David Richerby, Yuval Filmus, Evil, chi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    $\begingroup$ Your question is a programming question, and I think you should ask it on StackOverflow. $\endgroup$ – Christopher Bell II Jun 12 '17 at 21:52
-1
$\begingroup$
Pet(const string & the_name, const string & the_species): name(the_name), species(the_species), age(0)
  {

     if (species == "cats" || species == "dogs")
        throw std::invalid_argument( "received negative value" );
  }
$\endgroup$
  • 3
    $\begingroup$ Just to remind you, the strict programming questions are off-topic here. If this gets closed or migrated to SO, I think that some comment to the code or textual info would also be required to count it as the answer. $\endgroup$ – Evil Jun 12 '17 at 22:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.