C++ Exception Handling

Hi,
My question is about exception handling. I have the following code. I want to have exception on two conditions: 1)when the denominator for the division is "0". 2)when the denominator for the division is "1".
In case 1) I want to output "message1" of the class "myclass". In case 2) I want to output "message2" of the class "myclass".
But in the function "mydivision", I can only specify "myclass", but cannot specify the member "message1" or "message2". So in the "catch", always "message1" is selected. The second catch for "message2" never works. How can I use the second catch?
Pls let me know.
Thanks,
sunanda

#include <iostream.h>

class myclass
{
public:
myclass()
      : message("Dividing by zero!") {}
      const char *get_message1() const{ return message1; }
      const char *get_message2() const{ return message2; }
private:
      const char *message1;      
      const char *message2;
};


double mydivision(int up, int down)
{
if (down == 0)
throw myclass();  //I want to throw to "myclass's message1"

else if (down == 1)
throw myclass(); //I want to throw to "myclass's message2"

return static_cast<double> (up) / down;
}

int main()
{
int number1, number2;
double result;

cout<<"Enter two numbers: ";
while(cin>>number1>>number2)
{

try
      {
double result;
result = mydivision(number1, number2);
cout<<"The quotient is: "<<result<<endl;
      }
      
catch (myclass ex)
      {
cout<<"You are mistaken: "<<ex.get_message1()<<endl;
      }

catch (myclass ex)
      {
cout<<"You are mistaken: "<<ex.get_message2()<<endl;
      }
cout<<"Enter two numbers: ";
}

cout<<endl;
return 0;
}

      







      
skunduAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
Don't use the second catch. Instead, specify a parameter when throwing.

class myclass
{
public:
myclass(int w)
: message1("Dividing by zero!"), message2("Dividing by one!")
{
    which = w;
}

const char *get_message() const
{
    switch (which)
    {
        case 1:
            return message1;
        case 2:
            return message2;
        default:
            return NULL;
}

private:
const char *message1;
const char *message2;
int which;
};


double mydivision(int up, int down)
{
if (down == 0)
throw myclass(1);  //I want to throw to "myclass's message1"

else if (down == 1)
throw myclass(2); //I want to throw to "myclass's message2"

return static_cast<double> (up) / down;
}



try
{
double result;
result = mydivision(number1, number2);
cout<<"The quotient is: "<<result<<endl;
}
catch (myclass ex)
{
cout<<"You are mistaken: "<<ex.get_message()<<endl;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LucHoltkampCommented:
First:
You should catch the parameter with &
catch (myclass& ex)
This retains type information if the catched parameter is member of a class-hiarchy
Second:
It's neater to use seperate classes for each errorcondition:

struct XError
{
  XError(const char* s) : msg(x) {}
  const char* why() { return msg.c_str(); }
  private: string msg;
};

struct XDivideByZero : public XError
{
  XDivideByZero() : XError("Division by zero") {}
};

struct XDivideByOne : public XError
{
  XDivideByOne() : XError("Division by one") {}
};

Then you can catch and throw each error-condition seperatly, or you can use a single catch on XError

catch (XError& x)
{
  cout << x.why();
}

Luc
0
KangaRooCommented:
As Luc pointed out, using seperata classes for each exception is the C++ way.
0
chensuCommented:
>using seperata classes for each exception is the C++ way.

Not necessarily. In the real world cases, for example, a file exception can be caused by a lot of file operation errors. It is not realistic to use seperate exception classes for each error. In this case, use a member variable to indicate the error.
0
skunduAuthor Commented:
Thanks a lot to all of you. It worked properly...
sunanda
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.