[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

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;
}

      







      
0
skundu
Asked:
skundu
1 Solution
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now