custom C++ exceptions

Discussion in 'C++' started by slack_justyb, Jul 10, 2005.

  1. slack_justyb

    slack_justyb Guest

    Hello everyone,

    Recently I was coding a little app at home on my Linux box. I'm
    using gcc 3.3.4 (well actually g++) to compile my programs. I was
    compiling a module of the app into object code and got a nasty error
    that gcc could not find a matching constructor. Here is the offening
    code in simplified terms.


    ---code----

    //file: myexception.hh
    #include <exception>
    #include <string>

    using namespace std;

    class MyException : public exception {
    public:
    MyException(const string & Message = "") :
    exception(Message.c_str()) {}
    };

    //End of file.

    ---code---

    And simply put this is what I would like to do with it.

    ---code---

    #include <iostream>
    #include <myexception.hh>

    //And whatever other headers I may use.

    void blah() { throw MyException("Blah, not done yet!"); }

    int main() {

    //Some code here...

    try {
    blah()
    }


    catch (MyException e) {

    cout << e.what() << endl;
    }

    //and so on...
    }

    This seems to work with the HP-UX compiler and seems to be valid
    code in most of the books I have read, however, upon inspection of the
    header file on Linux, it does indeed lack a constructor for
    std::exception::exception(const char *)

    However, the GNU header seems to have a public function member
    called what(). This leads me to believe that somewhere it has a
    private data member that holds a message of some sort.

    This leads me to my question: If the GNU c++ library lacks a
    constructor to set a data member to hold some sort of message, then how
    does one set a message at all.

    Thank you all in advance,
    Justin
     
    slack_justyb, Jul 10, 2005
    #1
    1. Advertising

  2. slack_justyb wrote:
    > Hello everyone,
    >
    > Recently I was coding a little app at home on my Linux box. I'm
    > using gcc 3.3.4 (well actually g++) to compile my programs. I was
    > compiling a module of the app into object code and got a nasty error
    > that gcc could not find a matching constructor. Here is the offening
    > code in simplified terms.
    >
    >
    > ---code----
    >
    > //file: myexception.hh
    > #include <exception>
    > #include <string>
    >
    > using namespace std;
    >
    > class MyException : public exception {
    > public:
    > MyException(const string & Message = "") :
    > exception(Message.c_str()) {}
    > };
    >
    > //End of file.
    >
    > ---code---
    >
    > And simply put this is what I would like to do with it.
    >
    > ---code---
    >
    > #include <iostream>
    > #include <myexception.hh>
    >
    > //And whatever other headers I may use.
    >
    > void blah() { throw MyException("Blah, not done yet!"); }
    >
    > int main() {
    >
    > //Some code here...
    >
    > try {
    > blah()
    > }
    >
    >
    > catch (MyException e) {
    >
    > cout << e.what() << endl;
    > }
    >
    > //and so on...
    > }
    >
    > This seems to work with the HP-UX compiler and seems to be valid
    > code in most of the books I have read, however, upon inspection of the
    > header file on Linux, it does indeed lack a constructor for
    > std::exception::exception(const char *)
    >
    > However, the GNU header seems to have a public function member
    > called what(). This leads me to believe that somewhere it has a
    > private data member that holds a message of some sort.
    >
    > This leads me to my question: If the GNU c++ library lacks a
    > constructor to set a data member to hold some sort of message, then how
    > does one set a message at all.
    >
    > Thank you all in advance,
    > Justin
    >


    Here's a snip from the docs for 'std::exception'

    <snip>

    exception

    class exception {
    public:
    exception() throw();
    exception(const exception& rhs) throw();
    exception& operator=(const exception& rhs) throw();
    virtual ~exception() throw();
    virtual const char *what() const throw();
    };

    The class serves as the base class for all exceptions thrown by certain
    expressions and by the Standard C++ library. The C string value returned
    by what() is left unspecified by the default constructor, but may be
    defined by the constructors for certain derived classes. None of the
    member functions throw any exceptions.

    </snip>

    As you can see, there is not a constructor for 'exception' that
    takes a 'const char *' arg.

    what() is virtual, derived class can define a what() that returns
    meaningful data -and- implement a constructor that takes a
    'const char *' arg.

    Something like this might be appropriate:

    class MyException : public exception
    {
    private:
    std::string msg;

    public:
    MyException() throw() {}
    ~MyException() throw() {}
    MyException(const MyException& rhs) throw() :
    msg(rhs.msg) {}
    MyException(const char * Message) : msg(Message) {}
    MyException(const string & Message) : msg(Message) {}
    MyException& operator=(const MyException& rhs) throw()
    { msg = rhs.msg; }
    const char * what() const throw()
    { return msg.c_str(); }
    };

    Larry
     
    Larry I Smith, Jul 10, 2005
    #2
    1. Advertising

  3. slack_justyb

    slack_justyb Guest

    Thank you.

    I reread the header and noticed the virtual nature of the header.
    I'll move on to futher implement my exception class in order to make it
    more usable.

    Cheers,
    Justin
     
    slack_justyb, Jul 10, 2005
    #3
  4. slack_justyb

    Ian Guest

    slack_justyb wrote:
    >
    >
    > catch (MyException e) {
    >
    > cout << e.what() << endl;
    > }

    Not answering your question, but never catch by value. You will both
    slice any derived exception and copy it.

    Catch by const reference.

    Ian
     
    Ian, Jul 10, 2005
    #4
  5. slack_justyb

    slack_justyb Guest

    Thank you for your information.
    I was just trying to write some quick code to show my point in my
    example above, but to be more correct I will show the modified code
    here for any who may read this thread.

    ---code---

    catch (const MyException &e) {

    cout << e.what() << endl;
    }

    ---code---

    It was a slight mistake that I simply overlooked in writing my example.
    I hope that this clears things up.
     
    slack_justyb, Jul 10, 2005
    #5
    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. Chris Dunaway

    Custom Exceptions with Web Services

    Chris Dunaway, Jan 13, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    438
    Shiv Kumar
    Jan 13, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,126
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,065
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    637
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    715
Loading...

Share This Page