Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exception class and Errors 1: a) Write a snippet of code to declare (what would

ID: 3833545 • Letter: E

Question

Exception class and Errors

1: a) Write a snippet of code to declare (what would go into the .h file) and then implement (what would go in the .cpp file) as exception class called PetsBiteException which holds an error message, and a method called errorHandler which prints a message to a log file errorLog. The message is set by the custom constructor. Assume that the log file has been already opened, and is globally available.

b) Write a snippet of code to declare (.h) and then implement (.cpp) a public void method called treatPet scoped to PetHospital class, which takes a reference to Pet object, and throws PetBitesException if a Pet’s mood attributes is set to “annoyed”. The exception needs to output a message identifying the Pet where pets name is an attribute in the Pet class. assume that the rest of the PetHospital class and the Pet class with all its attributes have already been implemented.

c) Finally write a try/catch block of code, which invokes the treatPet method, catches the PetBitesException and then invokes the errorHandler method on the exception object to log the error message.

Please put each snippet of code with their respective question. Thank You!

Explanation / Answer

PROGRAM CODE:

1a)class PetsBiteException: public exception
{
   private:
   string message;
   public:
   PetsBiteException(string msg)
   {
       message = msg;
   }
  
   public void errorHandler()
   {
       log<<message<<endl;
   }
};

1b)

class PetHospital
{
   public:
   void treatPet(Pet pet)
   {
       if(pet.mood == "annoyed")
       {
           throw PetsBiteException("Pet " + pet.name() + " is annoyed");
       }
   }
};

1c)

try
   {
       PetHospital petHospital;
       Pet pet("Julie", "annoyed"); //name and mood of the pet
       petHospital.treatPet(pet);
   }catch(PetsBiteException &e)
   {
       e.errorHandler();
   }