Exception Handling (Exception keeps printing, ignoring other functions)

The primary issue is my exception within my test function continuously prints the exception, but ignores the rest of the functions within the try {}block and prints the exception, no matter how i change the conditions.

So in a separate Header file, I've created an exception class DataSizeFullException. This will not all although me to add a value to an array once the size is greater than capacity.

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

class DataSizeFullException{
    public:
        void print(){
        cerr<<"Error "<< errorCode << ": " <<Message<<endl;
        }
        DataSizeFullException(){
        errorCode=1000;
        Message = "You've reached the maximum capacity.";
        }
        //virtual ~DataSizeFullException(){
        //}
    private:
        int errorCode;
        string Message;
};

#endif // DATASIZEFULLEXCEPTION_H_INCLUDED


This is the function where DataSizeFullException gets thrown into.

1
2
3
4
5
6
7
8

void Statistics::add (double value) throw (DataSizeFullException){
    if (size<capacity){     //conditions for the exception
    pData[size]=value;
    size++;
    }
    else {throw DataSizeFullException();}
}


And this is my static void test function where I implement try and catch. The issue I'm having is that between the brackets of try and catch. My DataSizeFullException seems to ignore the rest of the functions with in the block and continues to print the exception, no matter the way i change the conditions. Statistics is another class where i'm implementing the DataSizeFullException.

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

void Statistics::test(){
    int capacity;
    capacity =10;
    Statistics aStatistics = Statistics(capacity);
    Statistics bStatistics = Statistics(capacity);
    try{aStatistics.print();   //try block where the the following functions are ignored and 
    aStatistics.add(10);       //then simply prints the exception, ignoring conditions.
    aStatistics.add(15);
    aStatistics.add(20);
    aStatistics.add(25);
    aStatistics.printStats();
    bStatistics.add(3);
    bStatistics.add(6);
    bStatistics.add(9);
    bStatistics.add(12);
    bStatistics.add(15);
    bStatistics.printStats();}
    catch(DataSizeFullException e){
        e.print();}

}


I've also incorporated the throw statement within my Statistics header file

1
2
3
4
class Statistics{
public:
void add (double value) throw (DataSizeFullException);
....}
Last edited on
What is the value of size when you call test?
Did you forget to initialise size?

Note that throwing dynamic exception specification was deprecated in C++11 and removed by C++17.
http://en.cppreference.com/w/cpp/language/except_spec
Well in my add function, size is supposed to increment within the add function. I did nonetheless attempt your recommendation and the issue still remains.
What is the value of size when you call add?
What is the value of capacity when you call add?

Add this line right at the beginning of add:
std::cout << "Statistics::add size == " << size << " capacity == " << capacity << '\n' ;
Topic archived. No new replies allowed.