¿Cómo crear excepciones?

Por lo tanto, tengo una próxima tarea que trata sobre las excepciones y las uso en mi programa actual de la libreta de direcciones en la que se centra la mayor parte de la tarea. Decidí jugar con las excepciones y todo el intento, y usar un diseño de clase, que es lo que eventualmente tendré que hacer para mi asignación en un par de semanas. Tengo un código de trabajo que verifica la excepción muy bien, pero lo que quiero saber es si hay una manera de estandarizar mi función de mensaje de error, (es decir, mi qué () llamar):

Aquí está mi código:

#include <iostream>
#include <exception>
using namespace std;


class testException: public exception
{
public:
  virtual const char* what() const throw() // my call to the std exception class function (doesn't nessasarily have to be virtual).
  {
  return "You can't divide by zero! Error code number 0, restarting the calculator..."; // my error message
  }

  void noZero();

}myex;  //<-this is just a lazy way to create an object



int main()
{
void noZero();
int a, b;

cout << endl;

cout << "Enter a number to be divided " << endl;

cout << endl;

cin >> a;

cout << endl;

cout << "You entered " << a << " , Now give me a number to divide by " << endl;

cin >> b;

try
{    
    myex.noZero(b); // trys my exception from my class to see if there is an issue
}
catch(testException &te) // if the error is true, then this calls up the eror message and restarts the progrm from the start.
{
    cout << te.what() << endl;
    return main();
}

cout <<endl;

cout << "The two numbers divided are " << (a / b) << endl;  // if no errors are found, then the calculation is performed and the program exits.

return 0;

}

  void testException::noZero(int &b) //my function that tests what I want to check
  { 
    if(b == 0 ) throw myex;  // only need to see if the problem exists, if it does, I throw my exception object, if it doesn't I just move onto the regular code.
  }

Lo que me gustaría poder hacer es hacerlo para que mi función what () pueda devolver un valor dependiendo de qué tipo de error se está detectando. Entonces, por ejemplo, si estuviera llamando un error que parecía ser el número superior, (a), para ver si era un cero, y si lo era, entonces se configuraría el mensaje para decir que "no se puede tener un numerador de cero ", pero aún así estar dentro de la función what (). Aquí hay un ejemplo:

  virtual const char* what() const throw() 
  if(myex == 1)
  {
      return "You can't have a 0 for the numerator! Error code # 1 "
  }
  else

  return "You can't divide by zero! Error code number 0, restarting the calculator..."; // my error message
  }

Obviamente, esto no funcionaría, pero ¿hay alguna forma de hacerlo para que no escriba una función diferente para cada mensaje de error?

Respuestas a la pregunta(4)

Su respuesta a la pregunta