Enums and exceptions in C++

Discussion in 'C++' started by pazabo@gmail.com, Dec 5, 2006.

  1. Guest

    Hi,

    I'm trying to create a class "Exception" that will contain some
    enumeration specifying cause (why the exception was thrown):

    class Exception {
    public:
    enum Type {
    DATABASE_CORRUPTED,
    COULD_NOT_READ_TABLE_MAP
    } type;

    explicit Exception(Type type) : type(type) {}
    };

    But I don't really know how does the compiler understand:

    throw Exception::COULD_NOT_READ_TABLE_MAP

    because it doesn't show any errors or even warnings, and
    COULD_NOT_READ_TABLE_MAP is not a member of Exception class. Does
    anybody know how can this be correct ? Also I would be greatful if you
    would share with me your experiences with C++ exceptions (do you create
    a class for every situation or do you try to do it like I do / how do
    you organize them).

    Paul PAZABO Zaborski
    , Dec 5, 2006
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > Hi,
    >
    > I'm trying to create a class "Exception" that will contain some
    > enumeration specifying cause (why the exception was thrown):
    >
    > class Exception {
    > public:
    > enum Type {
    > DATABASE_CORRUPTED,
    > COULD_NOT_READ_TABLE_MAP
    > } type;
    >
    > explicit Exception(Type type) : type(type) {}
    > };
    >
    > But I don't really know how does the compiler understand:
    >
    > throw Exception::COULD_NOT_READ_TABLE_MAP
    >
    > because it doesn't show any errors or even warnings, and
    > COULD_NOT_READ_TABLE_MAP is not a member of Exception class. Does
    > anybody know how can this be correct ? Also I would be greatful if you
    > would share with me your experiences with C++ exceptions (do you create
    > a class for every situation or do you try to do it like I do / how do
    > you organize them).
    >


    Actually, COULD_NOT_READ_TABLE_MAP *is* a member of Exception. What
    else would it be a member of? So it throws an Exception::Type.

    However, if you're trying to throw an Exception, you'll need to do this:

    throw Exception(Exception::COULD_NOT_READ_TABLE_MAP);
    red floyd, Dec 5, 2006
    #2
    1. Advertising

  3. Ondra Holub Guest

    napsal:
    > Hi,
    >
    > I'm trying to create a class "Exception" that will contain some
    > enumeration specifying cause (why the exception was thrown):
    >
    > class Exception {
    > public:
    > enum Type {
    > DATABASE_CORRUPTED,
    > COULD_NOT_READ_TABLE_MAP
    > } type;
    >
    > explicit Exception(Type type) : type(type) {}
    > };
    >
    > But I don't really know how does the compiler understand:
    >
    > throw Exception::COULD_NOT_READ_TABLE_MAP
    >
    > because it doesn't show any errors or even warnings, and
    > COULD_NOT_READ_TABLE_MAP is not a member of Exception class. Does
    > anybody know how can this be correct ? Also I would be greatful if you
    > would share with me your experiences with C++ exceptions (do you create
    > a class for every situation or do you try to do it like I do / how do
    > you organize them).
    >
    > Paul PAZABO Zaborski


    Try it this way:

    class Exception {
    public:
    typedef enum {
    DATABASE_CORRUPTED,
    COULD_NOT_READ_TABLE_MAP
    } Type;

    Type type_;

    explicit Exception(Type type) : type_(type) {}

    };

    And then throw it this way:

    throw Exception(DATABASE_CORRUPTED);
    Ondra Holub, Dec 5, 2006
    #3
  4. Guest

    On Dec 5, 7:29 pm, red floyd <> wrote:
    > wrote:
    > > Hi,

    >
    > > I'm trying to create a class "Exception" that will contain some
    > > enumeration specifying cause (why the exception was thrown):

    >
    > > class Exception {
    > > public:
    > > enum Type {
    > > DATABASE_CORRUPTED,
    > > COULD_NOT_READ_TABLE_MAP
    > > } type;

    >
    > > explicit Exception(Type type) : type(type) {}
    > > };

    >
    > > But I don't really know how does the compiler understand:

    >
    > > throw Exception::COULD_NOT_READ_TABLE_MAP

    >
    > > because it doesn't show any errors or even warnings, and
    > > COULD_NOT_READ_TABLE_MAP is not a member of Exception class. Does
    > > anybody know how can this be correct ? Also I would be greatful if you
    > > would share with me your experiences with C++ exceptions (do you create
    > > a class for every situation or do you try to do it like I do / how do
    > > you organize them).Actually, COULD_NOT_READ_TABLE_MAP *is* a member of Exception. What

    > else would it be a member of? So it throws an Exception::Type.
    >
    > However, if you're trying to throw an Exception, you'll need to do this:
    >
    > throw Exception(Exception::COULD_NOT_READ_TABLE_MAP);


    Ok, thanks :)

    Paul PAZABO Zaborski
    , Dec 5, 2006
    #4
  5. Salt_Peter Guest

    wrote:
    > Hi,
    >
    > I'm trying to create a class "Exception" that will contain some
    > enumeration specifying cause (why the exception was thrown):
    >
    > class Exception {
    > public:
    > enum Type {
    > DATABASE_CORRUPTED,
    > COULD_NOT_READ_TABLE_MAP
    > } type;
    >
    > explicit Exception(Type type) : type(type) {}
    > };
    >
    > But I don't really know how does the compiler understand:
    >
    > throw Exception::COULD_NOT_READ_TABLE_MAP
    >
    > because it doesn't show any errors or even warnings, and
    > COULD_NOT_READ_TABLE_MAP is not a member of Exception class. Does
    > anybody know how can this be correct ? Also I would be greatful if you
    > would share with me your experiences with C++ exceptions (do you create
    > a class for every situation or do you try to do it like I do / how do
    > you organize them).
    >
    > Paul PAZABO Zaborski


    One suggestion might be to make your own exceptions by deriving from
    std::exception.
    Or alternately, by deriving from one of std::exception's derivatives
    like std::runtime_error.

    That way, you can use a universal catch block to capture any
    std::exception, including your own.
    std::exception already has a virtual destructor and its already
    equipped with a what() non-throwing member function. So its a very
    simple construct (change the class names to your liking):

    #include <iostream>
    #include <ostream>
    #include <stdexcept>

    class db_corrupted_error : public std::runtime_error
    {
    public:
    db_corrupted_error()
    : std::runtime_error("database corrupted") { }
    };

    class read_table_error : public std::runtime_error
    {
    public:
    read_table_error()
    : std::runtime_error("read table map error") { }
    };

    int main()
    {
    try {

    // do stuff
    throw db_corrupted_error(); // testing

    } catch ( const std::exception& r_e ) {
    std::cerr << "error: " << r_e.what();
    std::cerr << std::endl;
    }
    }

    /*
    error: database corrupted
    */

    Another option is to derive from std::logic_error which is also a
    derivative of std::exception.
    Salt_Peter, Dec 5, 2006
    #5
  6. mlimber Guest

    Ondra Holub wrote:
    > Try it this way:
    >
    > class Exception {
    > public:
    > typedef enum {
    > DATABASE_CORRUPTED,
    > COULD_NOT_READ_TABLE_MAP
    > } Type;


    This is C-style and is unnecessary in C++. Just do:

    enum Type {
    DATABASE_CORRUPTED,
    COULD_NOT_READ_TABLE_MAP
    };


    >
    > Type type_;
    >
    > explicit Exception(Type type) : type_(type) {}
    >
    > };
    >
    > And then throw it this way:
    >
    > throw Exception(DATABASE_CORRUPTED);


    Of course you mean:

    throw Exception(Exception::DATABASE_CORRUPTED);

    But see Salt Peter's advice elsethread.

    Cheers! --M
    mlimber, Dec 5, 2006
    #6
    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. Ahmed Moustafa
    Replies:
    5
    Views:
    29,992
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,009
    Alex Martelli
    Nov 12, 2003
  3. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,105
    Keith Thompson
    Jan 20, 2007
  4. Replies:
    3
    Views:
    599
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    601
Loading...

Share This Page