title: How to create exceptions?
body: <p>So I have an upcoming assignment dealing with exceptions and using them in my current address book program that most of the homework is centered around. I decided to play around with exceptions and the whole try catch thing, and using a class design, which is what I will eventually have to do for my assignment in a couple of weeks. I have working code that check the exception just fine, but what I want to know, is if there is a way to standardize my error message function, (i.e my what() call):</p>
  
  <p>Here s my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;exception&gt;
  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;  //&lt;-this is just a lazy way to create an object
  
  
  
  int main()
  {
  void noZero();
  int a, b;
  
  cout &lt;&lt; endl;
  
  cout &lt;&lt; "Enter a number to be divided " &lt;&lt; endl;
  
  cout &lt;&lt; endl;
  
  cin &gt;&gt; a;
  
  cout &lt;&lt; endl;
  
  cout &lt;&lt; "You entered " &lt;&lt; a &lt;&lt; " , Now give me a number to divide by " &lt;&lt; endl;
  
  cin &gt;&gt; b;
  
  try
  {    
      myex.noZero(b); // trys my exception from my class to see if there is an issue
  }
  catch(testException &amp;te) // if the error is true, then this calls up the eror message and restarts the progrm from the start.
  {
      cout &lt;&lt; te.what() &lt;&lt; endl;
      return main();
  }
  
  cout &lt;&lt;endl;
  
  cout &lt;&lt; "The two numbers divided are " &lt;&lt; (a / b) &lt;&lt; endl;  // if no errors are found, then the calculation is performed and the program exits.
  
  return 0;
  
  }
  
    void testException::noZero(int &amp;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.
    }
  </code></pre>
  
  <p>What I would like to be able to do is make it so my what() function can return a value dependent on what type of error is being called on. So for instance, if I were calling up an error that looked a the top number,(a), to see if it was a zero, and if it was, it would then set the message to say that "you can't have a numerator of zero", but still be inside the what() function. Here's an example:</p>
  
  <pre><code>  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
    }
  </code></pre>
  
  <p>This obviously wouldn't work, but is there a way to make it so I'm not writing a different function for each error message?</p>