Confused with Exceptions

This is a discussion on Confused with Exceptions within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm trying to get my program to throw exceptions which will be caught by a separate program using my ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Confused with Exceptions

    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());
    };
    
    #endif
    Code:
    #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;
        }
    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"; }
    }
    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?

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your code is:
    Code:
    if (length < 2) throw std::exception();
    but at that point length has not been initialized, it should probably be:
    Code:
    if (num < 2) throw std::exception();
    or even better (since you keep using length later) would be to initialize length with num before your constructor code.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, the program is running now until the try statements in the test program. When it gets to the first one it just says abort instead of outputting the error messages. Now what am I doing wrong? Thanks for the help so far.

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    >> PolyLine(double xVals[], double yVals[], int num) throw(std::exception());

    I don't do exception handling a lot but when I do I never have to declare the function that it will throw an exception. So I *deleted* "throw(std::exception())" from your code and just have the following for the declaration and definition

    PolyLine(double xVals[], double yVals[], int num)

    Here's the output that I got when I run it
    Code:
    (1,1):(2,-3):(3,3):(4,-1)
    
    (1,1):(2,-3)
    4
    -3
    1.5
    0
    1
    1
    0
    -1
    0
    Exception was caught- 1 point
    Press any key to continue
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your exception specifications don't need the parentheses after std::exception, so if you want to leave the specifications in there, try removing the parentheses... the compiler might think that your constructor is throwing an exception that is different than the type specified in the exception specification, which leads to an abort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are Exceptions effective?
    By Petike in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2008, 01:23 AM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 03:12 AM
  3. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 02:49 PM
  4. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 10:21 AM
  5. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21