std::exception .. more

M

ma740988

The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure,
runtime_error and bad_exception
runtime_error and logic_error has subsets.

The question: If I threw all seven exceptions highlighted above.
std::exception is the only exception needed within the catch clause?
In other words I wouldn't need a 'catch all'? So now:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exception& err)
{
err.what();
}
}

No need for:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exception& err)
{
err.what();
}
catch (...)
{
// just in case.
}
}

I realize I could provide my own named exception but I'm just trying
to ensure I understand what I'm reading about std::exception:

-------------------------
Not sure where in my readings I may have missed this, nonetheless, with
deference to options 1 and 2 below, I dont think I fully understand
why std::fill does not work with option 2?


// Option 1
struct my_struct {
int my_vec[65535];
my_struct()
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }
};

//Option 2.
typedef std::vector<int> myIntVec;
struct my_struct2 {
myIntVec my_vec[65535];
my_struct2()
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }
};

I understand the obvious - i.e lets add a third option:

// Option 3
struct my_struct3 {
myIntVec my_vec;
my_struct3()
: my_vec(65535, 0x555) {}
};

Thanks in advance.
 
M

Mike Wahler

ma740988 said:
The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure,
runtime_error and bad_exception
runtime_error and logic_error has subsets.

The question: If I threw all seven exceptions highlighted above.
std::exception is the only exception needed within the catch clause?

It depends upon what level of distinction you need,
and portability.
In other words I wouldn't need a 'catch all'?

Since these types form an inheritance heirarchy, catching
a reference to the base type will catch all derived types.
So now:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exception& err)
{
err.what();
}
}

No need for:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exception& err)
{
err.what();
}
catch (...)
{
// just in case.
}
}

Again, it depends upon what level of distinction you need.
The 'std::exception'-derived types all have specific,
standardized names. The string returned by 'what()', however,
can and will vary among implementations (i'm not sure, but
I believe a valid return from 'what()' could be an empty
string.

So this means with the first method, you can exactly
distinguish portably among the types, but using 'what()',
you'll need to test for the string values, which can
change among implementations, and possibly between
versions of the same implementation.

I realize I could provide my own named exception but I'm just trying
to ensure I understand what I'm reading about std::exception:

What are you reading?
-------------------------
Not sure where in my readings I may have missed this, nonetheless, with
deference to options 1 and 2 below, I dont think I fully understand
why std::fill does not work with option 2?


// Option 1
struct my_struct {
int my_vec[65535];
my_struct()
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }
};

//Option 2.
typedef std::vector<int> myIntVec;
struct my_struct2 {
myIntVec my_vec[65535];

This is an *array* of vectors.
my_struct2()
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }

You're passing the address of my_vec[0], i.e. the
address of a vector. 0x555 does not qualify as
the 'value' of a vector.
};

I understand the obvious - i.e lets add a third option:

Do you? ;-)
// Option 3
struct my_struct3 {
myIntVec my_vec;
my_struct3()
: my_vec(65535, 0x555) {}
};

This last is what I'd do. It is true initialization,
the previous examples are not (the vector is default initialized,
then filled after the fact).

-Mike
 
J

John Harrison

ma740988 said:
The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failure,
runtime_error and bad_exception
runtime_error and logic_error has subsets.

The question: If I threw all seven exceptions highlighted above.
std::exception is the only exception needed within the catch clause?
In other words I wouldn't need a 'catch all'? So now:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exception& err)
{
err.what();
}
}

No need for:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exception& err)
{
err.what();
}
catch (...)
{
// just in case.
}
}

I realize I could provide my own named exception but I'm just trying
to ensure I understand what I'm reading about std::exception:

You understand right.
-------------------------
Not sure where in my readings I may have missed this, nonetheless, with
deference to options 1 and 2 below, I dont think I fully understand
why std::fill does not work with option 2?


// Option 1
struct my_struct {
int my_vec[65535];
my_struct()
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }
};

//Option 2.
typedef std::vector<int> myIntVec;
struct my_struct2 {
myIntVec my_vec[65535];
my_struct2()
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }
};

Because my_vec is an array of vectors, not a single vector. This works

//Option 2a.
typedef std::vector<int> myIntVec;
struct my_struct2 {
myIntVec my_vec;
my_struct2() : my_vec(65535)
{ std::fill (&my_vec[0], &my_vec[65535], 0x555); }
};

john
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top