Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 5th, 2006, 05:55 PM
pazabo@gmail.com
Guest
 
Posts: n/a
Default 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_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

  #2  
Old December 5th, 2006, 06:05 PM
red floyd
Guest
 
Posts: n/a
Default Re: Enums and exceptions in C++

pazabo@gmail.com wrote:
Quote:
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);

  #3  
Old December 5th, 2006, 06:15 PM
Ondra Holub
Guest
 
Posts: n/a
Default Re: Enums and exceptions in C++

pazabo@gmail.com napsal:
Quote:
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);

  #4  
Old December 5th, 2006, 06:15 PM
pazabo@gmail.com
Guest
 
Posts: n/a
Default Re: Enums and exceptions in C++



On Dec 5, 7:29 pm, red floyd <no.s...@here.dudewrote:
Quote:
paz...@gmail.com wrote:
Quote:
Hi,
>
Quote:
I'm trying to create a class "Exception" that will contain some
enumeration specifying cause (why the exception was thrown):
>
Quote:
class Exception {
public:
enum Type {
DATABASE_CORRUPTED,
COULD_NOT_READ_TABLE_MAP
} type;
>
Quote:
explicit Exception(Type type) : type(type) {}
};
>
Quote:
But I don't really know how does the compiler understand:
>
Quote:
throw Exception::COULD_NOT_READ_TABLE_MAP
>
Quote:
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

  #5  
Old December 5th, 2006, 06:35 PM
Salt_Peter
Guest
 
Posts: n/a
Default Re: Enums and exceptions in C++


paz...@gmail.com wrote:
Quote:
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.

  #6  
Old December 5th, 2006, 07:05 PM
mlimber
Guest
 
Posts: n/a
Default Re: Enums and exceptions in C++

Ondra Holub wrote:
Quote:
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
};

Quote:
>
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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles