473,322 Members | 1,538 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,322 software developers and data experts.

std::exception .. more


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.

Nov 4 '05 #1
2 4527

"ma740988" <ma******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

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
Nov 4 '05 #2
ma740988 wrote:
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
Nov 4 '05 #3

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

Similar topics

3
by: Noah Roberts | last post by:
I am having some problems with deriving the std::exception class. My compiler (g++-2.95) works with it just fine, but it does in a lot of broken cases. I have a user/developer that can't compile...
4
by: Boogie El Aceitoso | last post by:
Hi, I have an exception class for winapi errors (returns a formated error mesage, usign ::GetLastError() et al, with what()). It looks like this: class WinapiException : public...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
3
by: __PPS__ | last post by:
Hi, I've read in documentation to different libraries that their exception classes aren't subclasses from std::exception, and a separate catch statement is required for their exceptions. Always, I...
4
by: Divick | last post by:
Hi all, I want to subclass std::exception so as to designate the type of error that I want to throw, out of my classes, and for that I need to store the messages inside the exception classes. I...
2
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: ...
3
by: Sendil kumar | last post by:
Hi All, Problem Stetement:I have a problem in getting stack trace when I ues std::exception. In my code, I allocate virtual memory for certain kind of processing and will throw the...
5
by: Miles | last post by:
Hello all, When I subclass std::exception and throw an instance of the subclass, when I call the what() method of the caught exception, it does not call the overridden method. I'm under the...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.