473,508 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

exception

My question pertains to exceptions and their destruction

Say I do this in my (pseudo) code;

class an_exception
{
// details/methods for exception etc.
~an_exception() { .... }; };

try
{
foo();
}
catch (const an_exception& ex)
{
// handle the exception

}
void foo() throw (an_exception)
{
if (something)
an_exception ex;
throw (ex);
}
At what point is the destructor for the an_exception object called? Is
a copy taken at the point of "throw" and the "ex" instance destroyed
before the throw statement executes?

the reason I ask is to clarify another piece of code I saw whereby the
exception is newed and then thrown. The caller catches a pointer to the
exception. something like;

throw (new an_exception());

and the calling code obviously changes to handle the throw.

Anyhows I'm more interested in the pseudo code for the moment just to
get a handle on what happens when the exception is thrown by value.

Sep 20 '05 #1
5 1785
that should be ...

class an_exception
{
// details/methods for exception etc.
~an_exception() { .... }; };
} /////////// FORGOT THIS IN FIRST POST...

Sep 20 '05 #2
Gr*****@nospam.com wrote:
that should be ...

class an_exception
{
// details/methods for exception etc.
~an_exception() { .... }; };
} /////////// FORGOT THIS IN FIRST POST...
Yes, but you have two '};' after the '....' in your d-tor so the other
closing brace doesn't seem necessary. OTOH you have your 'try-catch'
outside of any function, which is rather weird... Try posting real
code next time.


V
Sep 20 '05 #3
Gr*****@nospam.com wrote:
At what point is the destructor for the an_exception object called?
Whenever one of them gets destroyed.
Is a copy taken at the point of "throw" and the "ex" instance destroyed
before the throw statement executes?
Likely. There might also happen additional copies during the stack
unwinding.
the reason I ask is to clarify another piece of code I saw whereby the
exception is newed and then thrown. The caller catches a pointer to the
exception.
Never do that. There is no good reason to throw by pointer.
something like;

throw (new an_exception());

and the calling code obviously changes to handle the throw.

Anyhows I'm more interested in the pseudo code for the moment just to
get a handle on what happens when the exception is thrown by value.


It might be copied once or several times. It's platform dependant.

Sep 20 '05 #4
Rolf Magnus wrote:
Gr*****@nospam.com wrote:
The caller catches a pointer to the exception.


Never do that. There is no good reason to throw by pointer.


All: What, never?
Captain: No, never.
All: What, never?
Captain: Well, hardly ever!

The FAQ (and Gilbert and Sullivan) make one exception to your rule:
When in MFC-land, do as the Microsofties do and catch by pointer:

http://www.parashift.com/c++-faq-lit....html#faq-17.8

Cheers! --M

Sep 20 '05 #5
thanks for the replies....

"Say I do this in my (pseudo) code; "
Try posting real code next time.
try reading the post next time.

Never do that. There is no good reason to throw by pointer.


I thought that myself. It's not my code in any case but I'll point it
out to the owner.

Sep 21 '05 #6

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

Similar topics

4
2744
by: Nicolas Fleury | last post by:
Hi, I've made a small utility to re-raise an exception with the same stack as before with additional information in it. Since I want to keep the same exception type and that some types have very...
1
4197
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw...
11
3251
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
4
15797
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
44
4161
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
13467
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
1748
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
3
2499
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
7
5083
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
2
6957
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: ...
0
7115
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...
0
7321
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,...
1
7036
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...
0
7489
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...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
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...
0
1547
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
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...

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.