473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

Dec 5 '06 #1
5 5370
pa****@gmail.com 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);

Dec 5 '06 #2
pa****@gmail.com 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);

Dec 5 '06 #3


On Dec 5, 7:29 pm, red floyd <no.s...@here.dudewrote:
paz...@gmail.com 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 '06 #4

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

Dec 5 '06 #5
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

Dec 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
6
by: Noah Roberts | last post by:
I am having an interesting compilation error that makes me wonder if enums can conflict with each other. For example: class A { enum X { TEST, TEST2 }; public: ....
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
7
by: alunharford | last post by:
When I'm coding in Java, I might write the following (contrived) code: boolean b = SomeMethod(); switch (b) { case true: return 1; case false: return 0; default: throw new InternalError();
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.