bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

exception classes, multiple inheritence


From: Dean Hoover
Subject: exception classes, multiple inheritence
Date: Tue, 06 Feb 2001 10:17:12 GMT

I am attempting to implement some exception object classes
like the std ones and actually derived from them, but with the
optional ability to carry extra debugging information
(__FILE__, __LINE__)

I am including what I have so far at the end of this note.
I would like to be able to use these classes in libraries
I am writing, and have the users be able to either catch
these classes or anything appropriate from std, as they
choose. If they use my extended classes, they may be
able to get file and line information, if it was provided at
throw. I also am writing little helper macros so that the
__FILE__ and __LINE__ arguments are automatically
provided if DEBUG is defined. This way, one of these
objects can be thrown explicitly, or through the macro,
depending on the desired outcome.

Have any of you tried something like this before? Can
you offer an alternative approach? When I run the
code, it dumps core. I am using gcc 2.95.2 on linux.
Any ideas what's wrong? Is it my code or the compiler's
treatment of a thrown object with multiple inheritence?

Thanks in advance.

Dean

Here it is:

#define DEBUG

#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <string>

namespace gutils {

class Exception : public std::exception
{
public:
    Exception
    (
      const string& what_arg,
      const char* file = 0,
      unsigned long line = 0
    ) : e_what_(what_arg), file_(0), line_(line)
    {
        if (file)
            file_ = new std::string(file);
    }

    virtual ~Exception() { delete file_; }

    virtual const char* what() const { return e_what_.c_str(); }

    virtual const char* file() const
    {
        if (file_)
            return file_->c_str();

        return 0;
    }

    virtual unsigned long line() const { return line_; }

private:
    std::string e_what_;
    std::string* file_;
    unsigned long line_;
}; // class Exception

#ifdef DEBUG
#define EXCEPTION(WHAT) gutils::Exception((WHAT), __FILE__, __LINE__)
#else
#define EXCEPTION(WHAT) gutils::Exception((WHAT))
#endif
class LogicError : public std::logic_error, public gutils::Exception
{
public:
    LogicError
    (
      const string& what_arg,
      const char* file = 0,
      unsigned long line = 0
    ) : logic_error(what_arg), Exception(what_arg, file, line) {}

    virtual const char* what() const { return Exception::what(); }

    // virtual ~LogicError() { }
};

#ifdef DEBUG
#define LOGIC_ERROR(WHAT) gutils::LogicError((WHAT), __FILE__, __LINE__)

#else
#define LOGIC_ERROR(WHAT) gutils::LogicError((WHAT))
#endif

} // namespace gutils

//==============================================================================

//

int main()
{
    try
    {
        throw LOGIC_ERROR("fooey");
    }
    catch (gutils::Exception& obj)
    {
        std::cout << obj.file() << ": " << obj.what() << std::endl;
    }
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]