Enums and exceptions in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pazabo@gmail.com

    Enums and exceptions in C++

    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_CORRUP TED,
    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::COUL D_NOT_READ_TABL E_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

  • red floyd

    #2
    Re: Enums and exceptions in C++

    pazabo@gmail.co m 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_CORRUP TED,
    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::COUL D_NOT_READ_TABL E_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(Excep tion::COULD_NOT _READ_TABLE_MAP );

    Comment

    • Ondra Holub

      #3
      Re: Enums and exceptions in C++

      pazabo@gmail.co m 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_CORRUP TED,
      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::COUL D_NOT_READ_TABL E_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_CORRUP TED,
      COULD_NOT_READ_ TABLE_MAP
      } Type;

      Type type_;

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

      };

      And then throw it this way:

      throw Exception(DATAB ASE_CORRUPTED);

      Comment

      • pazabo@gmail.com

        #4
        Re: Enums and exceptions in C++



        On Dec 5, 7:29 pm, red floyd <no.s...@here.d udewrote:
        paz...@gmail.co m 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_CORRUP TED,
        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::COUL D_NOT_READ_TABL E_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(Excep tion::COULD_NOT _READ_TABLE_MAP );
        Ok, thanks :)

        Paul PAZABO Zaborski

        Comment

        • Salt_Peter

          #5
          Re: Enums and exceptions in C++


          paz...@gmail.co m 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_CORRUP TED,
          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::COUL D_NOT_READ_TABL E_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_er ror.

          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_er ror : public std::runtime_er ror
          {
          public:
          db_corrupted_er ror()
          : std::runtime_er ror("database corrupted") { }
          };

          class read_table_erro r : public std::runtime_er ror
          {
          public:
          read_table_erro r()
          : std::runtime_er ror("read table map error") { }
          };

          int main()
          {
          try {

          // do stuff
          throw db_corrupted_er ror(); // 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_erro r which is also a
          derivative of std::exception.

          Comment

          • mlimber

            #6
            Re: Enums and exceptions in C++

            Ondra Holub wrote:
            Try it this way:
            >
            class Exception {
            public:
            typedef enum {
            DATABASE_CORRUP TED,
            COULD_NOT_READ_ TABLE_MAP
            } Type;
            This is C-style and is unnecessary in C++. Just do:

            enum Type {
            DATABASE_CORRUP TED,
            COULD_NOT_READ_ TABLE_MAP
            };

            >
            Type type_;
            >
            explicit Exception(Type type) : type_(type) {}
            >
            };
            >
            And then throw it this way:
            >
            throw Exception(DATAB ASE_CORRUPTED);
            Of course you mean:

            throw Exception(Excep tion::DATABASE_ CORRUPTED);

            But see Salt Peter's advice elsethread.

            Cheers! --M

            Comment

            Working...