Problem with Custom Exception

Apr 23, 2013 at 4:41pm
All,

I need to create a custom exception called "IllegalSubscriptException" for a function called Rational that should let the function operator[] throw this exception if the denominator is neither 0 nor 1. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Rational.h
#ifndef RATIONL_H
#define RATIONL_H
#include <string>
using namespace std;

class Rational
{
public:
  Rational();
  Rational(int numerator, int denominator);
  int getNumerator() const;
  int getDenominator() const;
  Rational add(const Rational &secondRational) const;
  Rational subtract(const Rational &secondRational) const;
  Rational multiply(const Rational &secondRational) const;
  Rational divide(const Rational &secondRational) const;
  int compareTo(const Rational &secondRational) const;
  bool equals(const Rational &secondRational) const;
  int intValue() const;
  double doubleValue() const;
  string toString() const;
  int &operator[](int index);

private:
  int numerator;
  int denominator;
  static int gcd(int n, int d);
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Rational.cpp
#include "Rational.h"
#include "IllegalSubscriptClass.h"
#include <sstream> // Used in toString to convert numbers to strings

Rational::Rational()
{
  numerator = 0;
  denominator = 1;
}

Rational::Rational(int numerator, int denominator)
{
  int factor = gcd(numerator, denominator);
  this->numerator = ((denominator > 0) ? 1 : -1) * numerator / factor;
  this->denominator = abs(denominator) / factor;
}

int Rational::getNumerator() const
{
  return numerator;
}

int Rational::getDenominator() const
{
  return denominator;
}

// Find GCD of two numbers
int Rational::gcd(int n, int d)
{
  int n1 = abs(n);
  int n2 = abs(d);
  int gcd = 1;

  for (int k = 1; k <= n1 && k <= n2; k++)
  {
    if (n1 % k == 0 && n2 % k == 0)
      gcd = k;
  }

  return gcd;
}

Rational Rational::add(const Rational &secondRational) const
{
  int n = numerator * secondRational.getDenominator() +
    denominator * secondRational.getNumerator();
  int d = denominator * secondRational.getDenominator();
  return Rational(n, d);
}

Rational Rational::subtract(const Rational &secondRational) const
{
  int n = numerator * secondRational.getDenominator()
    - denominator * secondRational.getNumerator();
  int d = denominator * secondRational.getDenominator();
  return Rational(n, d);
}

Rational Rational::multiply(const Rational &secondRational) const
{
  int n = numerator * secondRational.getNumerator();
  int d = denominator * secondRational.getDenominator();
  return Rational(n, d);
}

Rational Rational::divide(const Rational &secondRational) const
{
  int n = numerator * secondRational.getDenominator();
  int d = denominator * secondRational.numerator;
  return Rational(n, d);
}

int Rational::compareTo(const Rational &secondRational) const
{
  Rational temp = subtract(secondRational);
  if (temp.getNumerator() < 0)
    return -1;
  else if (temp.getNumerator() == 0)
    return 0;
  else
    return 1;
}

bool Rational::equals(const Rational &secondRational) const
{
  if (compareTo(secondRational) == 0)
    return true;
  else
    return false;
}

int Rational::intValue() const
{
  return getNumerator() / getDenominator();
}

double Rational::doubleValue() const
{
  return 1.0 * getNumerator() / getDenominator();
}

string Rational::toString() const
{
  stringstream ss1, ss2;
  ss1 << numerator;
  ss2 << denominator;

  if (denominator == 1)
    return ss1.str() + "";
  else
    return ss1.str() + "/" + ss2.str();
}

int &Rational::operator[](int index)
{
	if (index == 0)
		return numerator;
	else if (index ==1)
		return denominator;
	else
	{
		throw runtime_error("Subscript out of range");
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// IllegalSubscriptClass.h
#ifndef ILLEGALSUBSCRIPTIONEXCEPTIONCLASS_H
#define ILLEGALSUBSCRIPTIONEXCEPTIONCLASS_H
#include <exception>
#include "Rational.h"

class IllegalSubscriptException: public exception
{
public:
	IllegalSubscriptException
		(const char* errMessage):errMessage_(errMessage){}
	const char* what() const throw() {return errMessage_;}

private:
	const char* errMessage_;
}
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// TestRationalClassWithExceptionHandling.cpp
#include <iostream>
#include "Rational.h"
#include "IllegalSubscriptClass.h"

int main()
{
	try
	{
		Rational r(2, 3);
		r[0] = 5; // Set numerator to 5
		r[1] = 6; // Set denominator to 6
		cout << "r[0] is " << r[0] << endl;
		cout << "r[1] is " << r[1] << endl;
		cout << "r.doubleValue() is " << r.doubleValue() << endl;
	}

	catch(IllegalSubscriptException &e)
	{
		cout << e.what() << endl;
	}
	return 0;
}


I'm getting errors in the function:

1
2
3
4
5
6
7
8
9
10
11
string Rational::toString() const
{
  stringstream ss1, ss2;
  ss1 << numerator;
  ss2 << denominator;

  if (denominator == 1)
    return ss1.str() + "";
  else
    return ss1.str() + "/" + ss2.str();
}


The variables ss1 and ss2 is showing message "Incomplete type is not allowed".
Also, I'm getting the compile errors:
1
2
3
4
5
TestRationalClassWithExceptionHandling.cpp
1>c:\users\anderson.anderson-pc\google drive\advanced c++\rational\rational\testrationalclasswithexceptionhandling.cpp(5): error C2628: 'IllegalSubscriptException' followed by 'int' is illegal (did you forget a ';'?)
1>c:\users\anderson.anderson-pc\google drive\advanced c++\rational\rational\testrationalclasswithexceptionhandling.cpp(6): error C3874: return type of 'main' should be 'int' instead of 'IllegalSubscriptException'
1>  Rational.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\sstream(10): error C2143: syntax error : missing ';' before 'namespace'


Can anybody help? Thanks!
Apr 23, 2013 at 4:58pm
> (did you forget a ';'?)
IllegalSubscriptClass.h line 16

> The variables ss1 and ss2 is showing message "Incomplete type is not allowed".
std::stringstream
Last edited on Apr 23, 2013 at 4:59pm
Apr 23, 2013 at 5:01pm
Hahahhaha did not see that!!!

But my Handling Exception does not work...

Does anybody know why?
Apr 23, 2013 at 6:40pm
does not work is not an error message
Topic archived. No new replies allowed.