473,770 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::exception .. more


The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failu re,
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::exce ption& err)
{
err.what();
}
}

No need for:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exce ption& 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 4558

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

The hierachy for standard exception lists:
bad_alloc, bad_cast, bad_typeid, logic_error, ios_base::failu re,
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::exce ption& err)
{
err.what();
}
}

No need for:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exce ption& 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::failu re,
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::exce ption& err)
{
err.what();
}
}

No need for:

void my_test()
{
try {
// throw any or all seven above:
}
catch(std::exce ption& 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
11038
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 the following code: class CFENException : public std::exception { std::string _what; public:
4
8083
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 std::exception { public:
3
3504
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? Is this because there could be an exception thrown when the code creates a std::exception? I would assume that is not the case. However, if I want to create a new exception class, derived from std::exception (say MyException) then how can I...
3
2321
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 don't understand why they do so and want to fix this feature (most of the time, I go to edit source), even in their docs they write like this: try { // Close the database db.close(0); // DbException is not subclassed from std::exception, so //...
4
6410
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 want to use std::string to do that so that I don't have to deal with all the hustle of dealing with char *'s but as listed in the page (see link) below, it is not advisable to use std::string in my exception classes. The rational given is not...
2
6993
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: parseMetadata.obj : error LNK2001: unresolved external symbol "public: __thiscall std::exception::exception(void)" (??0exception@std@@QAE@XZ) 2>libcpmt.lib(string.obj) : error LNK2001: unresolved external symbol "public: __thiscall...
3
7726
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 std::bad_alloc exception if memory could not be allocated. I have never handled the bad_alloc, so, what I expect here is a stack trace from windows showing the excact location where the exception was thrown. But, to my surprise it gives only the windows ...
5
6537
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 impression that std::exception declares what() as a virtual method, so I'm at a loss as to why the subclass's what() is not being called. For example, for this program: #include <exception>
3
6436
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. I am not satisfied with the underlying machinery of the solution though. I am an advanced C programmer and most do object-based programming in C++. Please do not reply to this article with references to basic material or obvious tips. An...
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.