Some basic question about OOP in Exception class

D

Daniel Koch

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
 
M

maverik

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
 
A

Andre Kostur

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.
 
A

Andrey Tarasevich

Andre said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top