### Problem with Custom Exception

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:

 ``12345678910111213141516171819202122232425262728293031`` ``````// Rational.h #ifndef RATIONL_H #define RATIONL_H #include 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 ``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127`` ``````// Rational.cpp #include "Rational.h" #include "IllegalSubscriptClass.h" #include // 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"); } } ``````

 ``1234567891011121314151617`` ``````// IllegalSubscriptClass.h #ifndef ILLEGALSUBSCRIPTIONEXCEPTIONCLASS_H #define ILLEGALSUBSCRIPTIONEXCEPTIONCLASS_H #include #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 ``````

 ``123456789101112131415161718192021222324`` ``````// TestRationalClassWithExceptionHandling.cpp #include #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:

 ``1234567891011`` ``````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:
 ``12345`` ``````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!
> (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
Hahahhaha did not see that!!!

But my Handling Exception does not work...

Does anybody know why?
does not work is not an error message
Topic archived. No new replies allowed.