I am still a beginner and I do not really grasp the purpose of exceptions in a program.
When you throw an exception, you basically skip a (small or big) part of your program, so one of my questions would be : should an exception leads to the end of the program ?
Also, suppose we have a class Rectangle. It would be define by its length and its height like this :
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle(double length, double height)
{
if (length > 0 and heigth > 0) {
length_ = length;
height_ = height;
}
else {
// Throw an exception ? Use a convention ?
}
}
// other methods
private:
double length_;
double height_;
};
int main() {
// do stuff
return 0;
}
However, it would not make much sense to have a negative length or height. So should I throw an exception in the constructor ? Should I use an arbitrary convention and take their absolute value ? Should I prevent the user in the main() from passing negative arguments ?
Aucun commentaire:
Enregistrer un commentaire