Some basic question about OOP in Exception class

Discussion in 'C++' started by Daniel Koch, Nov 25, 2008.

  1. Daniel Koch

    Daniel Koch Guest

    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
     
    Daniel Koch, Nov 25, 2008
    #1
    1. Advertising

  2. Daniel Koch

    maverik Guest

    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
     
    maverik, Nov 25, 2008
    #2
    1. Advertising

  3. Daniel Koch

    Andre Kostur Guest

    Daniel Koch <> wrote in
    news::

    > 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.
     
    Andre Kostur, Nov 25, 2008
    #3
  4. 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.
     
    Andrey Tarasevich, Nov 25, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,942
    Thomas Weidenfeller
    Oct 12, 2005
  2. Michael Satterwhite

    (Very) Basic question re: exception

    Michael Satterwhite, Apr 21, 2004, in forum: C++
    Replies:
    6
    Views:
    492
    John Carson
    Apr 21, 2004
  3. Nemisis

    Basic OOP questions for an expert

    Nemisis, Aug 8, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    320
    Nemisis
    Aug 8, 2006
  4. asdf
    Replies:
    1
    Views:
    370
    Ian Collins
    Nov 30, 2006
  5. Replies:
    3
    Views:
    156
    Elegie
    Sep 24, 2006
Loading...

Share This Page