Hello,

I'm trying to get my program to throw exceptions which will be caught by a separate program using my class. I've tried a number of different things, but none seemed to work. Here is the header file, class file, and the test file:

Code:#ifndef POLYLINE_H #define POLYLINE_H #include <iostream> #include <exception> class PolyLine { friend std::ostream & operator << (std::ostream & out, const PolyLine ob); double *x; double *y; int length; public: int points(); double value(double xNum); double max(); double max(double a, double b); double integral(double a, double b); PolyLine(double xVals[], double yVals[], int num) throw(std::exception()); }; #endifCode:#include "PolyLine.h" PolyLine::PolyLine (double xVals[], double yVals[], int num) throw(std::exception()){ if (length < 2) throw std::exception(); else { for (int i = 0; i < length - 1; i++) { if (xVals[i] >= xVals[i+1]) throw std::exception(); } } length = num; x = new double[length]; y = new double[length]; for (int i = 0; i < length; i++) { x[i] = xVals[i]; y[i] = yVals[i]; } } int PolyLine::points() { return length; } double PolyLine::value(double xNum) { double val = 0.0; double finalSpot = x[length - 1]; if (xNum < x[0] || xNum > finalSpot) return val; for (int i = 0; i < length; i++) { if (x[i] == xNum) val = y[i]; else if (x[i] != xNum && x[i] > xNum) val = xNum; } return val; } double PolyLine::max() { double max = 0.0; for (int i = 0; i < length; i++) if (y[i] > max) max = y[i]; return max; } double PolyLine::max(double a, double b) { double max = 0.0; double finalSpot = x[length - 1]; int i = 0; while (x[i] != a) { if (i == length - 1) return max; else if (x[i] > a) { if (y[i] < 0) break; else { max = y[i]; break; } } else i++; } while (x[i] != b) { if (i == length - 1 || x[i] > finalSpot || b < x[i]) return max; else if (y[i] > max) max = y[i]; i++; } return max; } double PolyLine::integral(double a, double b) { double integ = 0.0; double trapFormula = 0.0; int i = 0; int num = 0; while (x[i] != a) { if (i == length - 1) return integ; else i++; } while (x[i] != b && i != length - 1) { trapFormula = ((x[i+1] - x[i]))*((y[i]+y[i+1])/2); integ += trapFormula; i++; } return integ; } std::ostream & operator << (std::ostream & out, const PolyLine ob) { int finalSpot = ob.length - 1; for (int i = 0; i < finalSpot; i++) out << "(" << ob.x[i] << "," << ob.y[i] << "):"; out << "(" << ob.x[finalSpot] << "," << ob.y[finalSpot] << ")" << std::endl; return out; }I can't alter the test program code, but anything else is free game. When I compile and run these programs together, the a.out file simply says "Abort" when I try to display it. I'm trying to get the constructor to throw an exception if there are fewer than 2 members in the array or if the xVals array is not in ascending order. Thoughts?Code:#include <iostream> #include "PolyLine.h" using namespace std; int main() { double x[] = {1.0,2.0,3.0,4.0}, y[] = {1, -3, 3, -1}; PolyLine single (x, y, 2), //automatic *pl = new PolyLine(x, y, 4 ); // dynamic cout << *pl << endl; // (1,1):(2,-3):(3,3):(4,-1) cerr << single; // (1,1):(2,-3) // PolyLine plCopy ( single ); //"copy constructor" cout << pl->points() << endl; // number of points for this PolyLine (in this case, 4) cout << single.value( 2.0 ) << endl; // y-value corresponding to x cout << single.value( 1.5 ) << endl; // y-value corresponding to x cout << single.value( 0.0 ) << endl; // return 0.0 if x is out of range cout << single.max() << endl; cout << single.max(1.0,2.0) << endl; cout << single.max(1.5,2.5) << endl; cout << pl->integral( 1.0, 2.0 ) << endl; cout << pl->integral( 0.0, 1.5 ) << endl; // cout << pl->max( single ) << endl; // returns new PolyLine try { PolyLine trouble(x, y, 1); } catch (...) { cerr << "Exception was caught- 1 point\n"; } try { PolyLine trouble(y, x, 4); } catch (...) { cerr << "Exception was caught- x out of order\n"; } }