| re: std::exception .. more
"ma740988" <ma740988@gmail.com> wrote in message
news:1131074158.059719.121840@g47g2000cwa.googlegr oups.com...[color=blue]
>
> 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?[/color]
It depends upon what level of distinction you need,
and portability.
[color=blue]
> In other words I wouldn't need a 'catch all'?[/color]
Since these types form an inheritance heirarchy, catching
a reference to the base type will catch all derived types.
[color=blue]
>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.
> }
> }[/color]
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.
[color=blue]
>
> 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:[/color]
What are you reading?
[color=blue]
>
> -------------------------
> 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];[/color]
This is an *array* of vectors.
[color=blue]
> my_struct2()
> { std::fill (&my_vec[0], &my_vec[65535], 0x555); }[/color]
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.
[color=blue]
> };
>
> I understand the obvious - i.e lets add a third option:[/color]
Do you? ;-)
[color=blue]
>
> // Option 3
> struct my_struct3 {
> myIntVec my_vec;
> my_struct3()
> : my_vec(65535, 0x555) {}
> };[/color]
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 |