add error handling in my program

Hello all

I want to add error handling in my program but dont know what is the best way to do it.
Im using the cilkplus compiler which changes a little bit the way exceptions work and since im not too familiar with exceptions yet im thinking to use return values for error handling.

The thing is that the same errors occur in many different files.And all the classes are in namespaces.

I was thinking to write a new header file that defines all the error codes and include it in every file that uses them.
But is this the best way to do it ?Should i use #define or enum ?And should it be inside the namespace or not?

Thanks
I don't know the cilkplus compiler, but how different can exceptions work? Just do it with the try-catch block. Or what's the problem?
Hello

I started implementing the error handling with exceptions but since its my first time that i actually use them in my program( before it was just examples ) i have some questions about exceptions.

I read that i shouldn't use them from every kind of error that i have.
Im wondering if it is proper to use a function which throws an exception and also returns a value for checking.
For example this function
1
2
3
4
5
6
7
8
9
10
11
int open( const std::string& s , const char mode ) {

            if( !(file = fopen( s.c_str() , mode )))
                    return 0;

           doSomeWork();
           if( somethingHappend )
                  throw my_error();

          return 1;
}


So now i have to check the return value in main and also write the exception handler for my_error.
I dont want to throw exceptions for every if() that goes wrong but i dont know either if i should use the exceptions this way.

Can someone explain the way i should use exceptions?I know the way they work but i dont know what is the best way to use them.

Thanks
In your function you should throw an exception if opening the file fails, because it is something you have no influence on. An exception is basically an unexpected error.
And no, it makes no sense to throw an exception and return something afterwards, because as soon as you throw and exception, you break out of the whole try block and catch the exception. You don't return to where you come from afterwards. Either the program terminates or if the try-catch block is inside a loop, you start again at the top of the loop. Understand?

I would write this function like this:
1
2
3
4
5
6
7
8
9
void open( const std::string& s , const char mode ) {

            if( !(file = fopen( s.c_str() , mode )))
                    throw runtime_error("Error opening file");

           doSomeWork();
           if( somethingHappend )
                  throw my_error();
}

Since you throw an exception if an error occurs, there is no need to return if the function is successful or not (why not use bool instead of int anyway?). So if you use this function, you won't get past it, if there is an error so there is no need to check for success.
Thanks for the answer

I know i cant return from where threw the exception.But its not sure if the exception will be thrown.
But i think you are right.Its redundant to check the function twice ( in main with the return value and with the exception handler )

I only thought the overhead from all this throwing even for small problems.
But you are write about the open function since i cant do something about it inside the functions i should throw an exception.

A completely different question ( i just dont want to create a new topic. )
I have a static object in one of my class used to count the objects of this class and notify at the end if there is any object alive.
But i cant define it.
1
2
3
4
5
6
7
class a{
static int counter;
static AutoCounter<a> myCounter;
};
int a::counter = 0;
//how can i define it here in my cpp?
//AutoCounter<a> a::myCounter; //tried this but didnt work 

gives errors such as undefined reference to function AutoCounter<a>.something
How can i call the constructor for a static object?

Thanks a lot

For this I'll just point you to an article, since I don't fully understand the use of static either.
http://www.cprogramming.com/tutorial/statickeyword.html

Hope this helps.
AutoCounter<a> a::myCounter; works perfectly fine for me. Maybe there's a problem with AutoCounter class itself?

Can you provide the code for AutoCounter? Are all functions defined in the header file for AutoCounter? If the functions are defined in a cpp file, and there are no explicit template instantiations with correct template parameters in that file, you will get exactly the undefined reference errors.

This happens because while compiling the cpp file that uses AutoCounter, the compiler doesn't know the definitions of the functions of template class, and while compiling the cpp file with those definitions, it doesn't know which template parameters to expand these definitions with (and also, it doesn't see the reason to expand them as no function uses them in this translation unit).
Last edited on
Personally I use static local objects since you are know when they are instantiated (On first construction - forget multithreading for a minute). This way if you ever needed to access the static object from another static object you can control how this is done.

What I mean is this...

1
2
3
4
static AutoCounter<a>& GetAutoCounterInstance(){
    static AutoCounter<a> myCounter;
    return myCounter;
}


Then just call this function to construct and access. The constructor only gets called once and you know when, the first time it is called.

In your case it looks like you can get rid of the member myCounter and just add this method to your class and use it as if you were using the myCounter variable it'self.
Last edited on
Topic archived. No new replies allowed.