Enums and exceptions in C++

P

pazabo

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
 
R

red floyd

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);
 
O

Ondra Holub

(e-mail address removed) 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);
 
P

pazabo

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
 
S

Salt_Peter

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.
 
M

mlimber

Ondra said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Error codes vs. exceptions 135
Meme generator in c 1
Classes for enums 5
Catching exceptions 14
enums clash 9
Exceptions 1
enums in C and C++ 13
exceptions in constructor 4

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top