mardi 26 avril 2016

с++ shapes library exception handling

I want to write some kind of "Shapes library" program.

Not to write a lot of code, let's say we have just an abstract base class Shape and some derived classes, for example, Line, Rectangle and Circle (I think it's not so important to describe them).

Considering that the main function will be something like this:

int main() {

    Shape *rect = new Rectangle(Pixel(25, 10), Pixel(33, 15));
    Shape *line = new Line(Pixel(1, 12), Pixel(23, 12));
    Shape *circle = new Circle(Pixel(37, 12), 2);
    refresh_shapes(); // just draw every created shape
    cin.get();

    rect->rotate_left();
    refresh_shapes();
    cin.get();

    rect->move(45, 12);
    refresh_shapes();
    cin.get();

    line->up(rect); // place the line right above the rect
    refresh_shapes();
    cin.get();

    circle->down();
    refresh_shapes();
    cin.get();

    return 0;
}

how should I handle exceptions that might be thrown in constructors or maybe after moving (if shape is out of screen bounds)?

For example, if I wrap whole code in one try-catch block, I'll see on the screen everything that happened before throwing an exception. But I want to "save" as much as possible. Is there a better solution than wrapping almost every line in try-catch block?

Sorry for bothering, just want to understand how it works.

Aucun commentaire:

Enregistrer un commentaire