473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where is Exception Object's internal data stored?

Standard exception classes in C++ have what() which returns const char
* and
they have constructors accepting string.

Where is that string created and when is the string destroyed?

In the function below, e is a local object so when the function
terminates,
the internal string should be gone too, isn't it?

But doing "cout << exTest();" in another function still prints the
correct string!

const char * exTest()
{
std::out_of_ran ge e("Testing Testing Testing Testing Testing");
// cout << &e << endl;
// cout << (void *) e.what() << endl;
return e.what();
}
This makes me wonder where Exception objects are created and when they
are destroyed. Am I just lucky to access deallocated memory space
which has not been overwritten? Or are Exception objects are created
in a special space (Not stack, not heap).

If I am going to write my own exception classes with string messages,
where should I create the string message? When should I deallocate the
space? What if I get bad::alloc during the allocation of space for the
string?
How can standard exception clasess constructors guarantee throw()
specification?

I would very much appreciate any kind explanation on this matter. TIA
Jul 22 '05 #1
2 2421
"CoolPint" <co******@yahoo .co.uk> wrote...
Standard exception classes in C++ have what() which returns const char
* and
they have constructors accepting string.

Where is that string created and when is the string destroyed?
The Standard says that it's implementation-defined. Most likely it's
a constant NTBS, created when the program is loaded and never destroyed
(or, destroyed along with the program).
In the function below, e is a local object so when the function
terminates,
the internal string should be gone too, isn't it?

But doing "cout << exTest();" in another function still prints the
correct string!
It is unknown how std::out_of_ran ge stores the information internally.
Well, not unknown, but implementation-defined. If you must learn how
it does that, look in the source code for your copy of the library.

const char * exTest()
{
std::out_of_ran ge e("Testing Testing Testing Testing Testing");
Here a temporary string is constructed from a [permanently alive] string
literal and passed to 'e'. For all we know (or care), the string literal
is treated as 'const char*' and stored indirectly in the 'string' object.
The internal data of 'std::logic_err or' also stores the pointer instead
of copying the string, for example. Reference-counted implementations of
std::string are allowed.
// cout << &e << endl;
// cout << (void *) e.what() << endl;
return e.what();
For all we know, the stored 'std::string' object is now queried for its
'c_str()' and the original pointer (to the string literal) is obtained,
thus resulting in a pointer to something that is never destroyed.
}
This makes me wonder where Exception objects are created and when they
are destroyed. Am I just lucky to access deallocated memory space
which has not been overwritten? Or are Exception objects are created
in a special space (Not stack, not heap).
See above. It doesn't matter where the exceptions are created.

If I am going to write my own exception classes with string messages,
where should I create the string message? When should I deallocate the
space? What if I get bad::alloc during the allocation of space for the
string?
Those are good questions. You should probably read about implementing
your own exceptions in "The C++ Standard Library". Page 30 contains very
straight-forward recommendations .
How can standard exception clasess constructors guarantee throw()
specification?


They only use mechanisms that are known to never throw, I suppose.

HTH

V
Jul 22 '05 #2

"CoolPint" <co******@yahoo .co.uk> wrote in message
news:15******** *************** ***@posting.goo gle.com...
Standard exception classes in C++ have what() which returns const char
* and
they have constructors accepting string.

Where is that string created and when is the string destroyed?
It will be a member of the exception class created when the exception class
is constructed
and destroyed when it is destroyed.

In the function below, e is a local object so when the function
terminates,
the internal string should be gone too, isn't it?
What do you mean by 'gone'.
The standard definition is that it is undefined behaviour to use the pointer
that used
to point to it.

But doing "cout << exTest();" in another function still prints the
correct string!
If you use a debug memory allocation library you will find that it doesn't.
It is only there because the memory hasn't been reused for anything else
yet.

const char * exTest()
{
std::out_of_ran ge e("Testing Testing Testing Testing Testing");
// cout << &e << endl;
// cout << (void *) e.what() << endl;
return e.what();
}
This makes me wonder where Exception objects are created and when they
are destroyed. Am I just lucky to access deallocated memory space
which has not been overwritten?
Yes
Or are Exception objects are created
in a special space (Not stack, not heap).
No - or at least not when used as in your example.
There is nothing magic about the exception classes - they are just plain old
classes
that you could write yourself.

The magic comes when you throw an object (any object) and even
then it wouldn't justify what you are doing which would be the equivalent
of:
const char* foo()
{
try
{
throw out_of_range("s hkhsdhqsklhdlk" );
}
catch( exception& e)
{
return e.what(); // still undefined but may show the same
behaviour
}
}

The exception will be constructed in 'a special place' but the c-string in
it will still be on the heap as for any other std::string chars.
If I am going to write my own exception classes with string messages,
where should I create the string message? When should I deallocate the
Use a std::string member and it will all happen by magic.
space? What if I get bad::alloc during the allocation of space for the
string?
I bet you £1000000 it will never happen (not counting deliberately
trying to do it) and even if it did you're doomed anyway.
How can standard exception clasess constructors guarantee throw()
specification?

They haven't got one in my copy of the std - except for the ones that don't
take a std::string
in which case they obviously just use a static const char[].
I would very much appreciate any kind explanation on this matter. TIA

Jul 22 '05 #3

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

Similar topics

9
2677
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value type-stored on the stack Regards thomson
4
1443
by: Frank Rizzo | last post by:
Hello. Every now and then (it's rare), i get an exception when calling SqlDataAdapter.Fill. The code is calling a stored procedure and attempts to stuff the results into a dataset. It yields the following exception. I am trying to understand what the exception is actually complaining about. Is it a problem with the data that came back or is it an issue inside the stored procedure? Any alternative ways to nail down this issue would...
28
2087
by: jakk | last post by:
Hello All, I have a question about how to handle exceptions. Iam working on an ASP.NET application which has a presentation layer, Business Layer and DataAccess Layer. Iam confused about where to handle exceptions. Say forexample I have a requirement where certain value has to be between 30-72 only and Iam doing this validation in the the BL. if the value is not between 30-72 then Iam throwing an exception back to the UI layer. Is this...
9
2320
by: RalphTheExpert | last post by:
I'm getting different behavior if my code is running under the debugger or not. I have modified Winmain to look like this: // Copyright (C) 2002 Microsoft Corporation // All rights reserved. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
4
7234
by: Terry | last post by:
Hello, I am trying to get a response for an .aspx page in my current project (same virtual directory) by using WebRequest.GetResponse but I keep getting a exception with "500 Internal server error" in the exception message. I am able to do this fine with another .aspx page that has no code-behind. The page that has code-behind throws the exception. What I am doing is getting the .aspx response, reading the stream, replacing
5
1385
by: Sridhar | last post by:
Hi, I have created a web application. I have a data access dll to interact with database. I added this dll as a reference to the web application. Now let's say if I am calling a function in data access and if it throws exception (like cannot find stored procedure), how do I retrieve that exception message back in the web form? In the client, I am able to catch the exception, but the message I am getting is "Object reference not set to an...
10
4266
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version. Because of this, I am unable to store these settings in the App.Config file, as this gets updated every time the application does, and there doesn't appear to be a way of preventing this. Most of my application settings are kept in the...
6
2052
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in your business layer and/or data access layer? suppose in my data access layer, I provide try and catch, log the exception and re-throw it back to business layer, then in yoru business layer, what do you do? throw it back to the code behind or...
3
12061
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. (machine.config) ---> System.Security.SecurityException: Request for the permission of type
0
9522
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
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
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
9948
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
9902
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,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
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
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.