473,480 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Create 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 1783
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
2738
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
4195
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
3243
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
15791
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
4155
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
13464
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
1742
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
2497
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
5079
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
6951
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
7048
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,...
0
6911
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
7050
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,...
0
7091
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...
1
6743
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
2999
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
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
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
564
muto222
php
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.