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?
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
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.
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?
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.
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
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?
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).
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.