[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Some basic question about OOP in Exception class

Daniel Koch

11/25/2008 5:47:00 PM

Hi, I've this Exception class:

// exception.h file
#ifndef EXCEPTION_H
#define EXCEPTION_H

#include <glibmm.h>
#include <gtkmm/messagedialog.h>

class Exception
{
private:
Glib::ustring error_message;

public:
Exception(Glib::ustring s);
void DisplayError();

protected:
Glib::ustring getErrorMessage(){ return error_message; }
};

#endif

// exception.cpp file
#include "exception.h"

Exception::Exception(Glib::ustring s)
{
error_message = s;
}

void Exception::DisplayError()
{
// display dialog
Gtk::MessageDialog dialog("System Error", false, Gtk::MESSAGE_ERROR);

dialog.set_secondary_text(error_message);
dialog.run();
}


I need to identify when it is a System Error or when it is an
Application Error. Then it could be extended to a child class:
ApplicationException.

My question is about "System Error" MessageDialog's title. How it can
be implemented? How it can be changed when I'll define the
ApplicationError class?

Thank you,
Daniel Koch
3 Answers

maverik

11/25/2008 6:09:00 PM

0

My be you can define a base class exception as pure virtual and derive
other exceptions from this base class.

// exception.h file
#ifndef EXCEPTION_H
#define EXCEPTION_H

#include <glibmm.h>
#include <gtkmm/messagedialog.h>

class Exception {
public:
virtual Exception(Glib::ustring s) = 0;
virtual void DisplayError() = 0;

protected:
virtual Glib::ustring getErrorMessage() = 0;

};

class SystemException : public Exception {
public:
Exception(Glib::ustring s) { error_message = s};
virtual void DisplayError() { ... dialog(..., "System
error", ...); ... }

protected:
Glib::ustring getErrorMessage() {return error_message}
private:
Glib::ustring error_message;
};


class ApplicationException : public Exception {
public:
Exception(Glib::ustring s) { error_message = s};
virtual void DisplayError() { ... dialog(..., "Application
error", ...); ... }

protected:
Glib::ustring getErrorMessage() {return error_message}

private:
Glib::ustring error_message;

};

#endif

Andre Kostur

11/25/2008 6:42:00 PM

0

Daniel Koch <daniel.koch@gmail.com> wrote in
news:c9504ae1-a7ca-42d9-8c1a-4186b2020d6f@t2g2000yqm.googlegroups.com:

> Hi, I've this Exception class:
>
> // exception.h file
> #ifndef EXCEPTION_H
> #define EXCEPTION_H
>
> #include <glibmm.h>
> #include <gtkmm/messagedialog.h>
>
> class Exception
> {
> private:
> Glib::ustring error_message;
>
> public:
> Exception(Glib::ustring s);
> void DisplayError();
>
> protected:
> Glib::ustring getErrorMessage(){ return error_message; }
> };
>
> #endif
>
> // exception.cpp file
> #include "exception.h"
>
> Exception::Exception(Glib::ustring s)
> {
> error_message = s;
> }
>
> void Exception::DisplayError()
> {
> // display dialog
> Gtk::MessageDialog dialog("System Error", false,
> Gtk::MESSAGE_ERROR);
>
> dialog.set_secondary_text(error_message);
> dialog.run();
> }
>
>
> I need to identify when it is a System Error or when it is an
> Application Error. Then it could be extended to a child class:
> ApplicationException.
>
> My question is about "System Error" MessageDialog's title. How it can
> be implemented? How it can be changed when I'll define the
> ApplicationError class?

If you have SystemErrorException and ApplicationErrorException children
classes of Exception, you could declare a pure virtual method "virtual
const char * DialogTitle() const = 0". In the children classes implement
that method to return the appropriate title. In Display Error use:
"Gtk::MessageDialog dialog(DialogTitle(), false);". Don't forget to also
make your destructor virtual as well.

Andrey Tarasevich

11/25/2008 7:06:00 PM

0

Andre Kostur wrote:
> Don't forget to also make your destructor virtual as well.

If the "exception classes" in this case are the classes to be used in
'throw' expressions, then the only case when one might need virtual
destructors in this class hierarchy is when one's planning to work with
dynamically allocated exception objects. Allocating exception objects
dynamically (i.e. throwing pointers to exception objects) is a rather
questionable practice, I'd say. If I was to write that hierarchy, I'd
make a point of _not_ declaring a virtual destructor and supplying the
whole thing with a comment saying that this is done (or, more precisely,
not done) intentionally.