473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

try object out of scope before catch?

I have a been testing the try/catch exception mechanism
(as implemented by DigitalMars) and found that if I
throw an object its dtor gets called 3 times, if I throw
a reference its dtor gets called twice - and so it seems
practical to instantiate an object and throw its address,
and sure enough, its dtor gets called only once.

This is part of the Test class implementation (contains only
one data member: "int n" ):

volatile int count = 0;

// Constructor
Test::Test() : n(0) {
++::count;
std::cout << " Test(" << ::count << ")\n";
}

// Constructor
Test::Test(int nn) : n(nn) {
++::count;
std::cout << " Test(" << ::count << ")\n";
}

// Destructor
Test::~Test() {
std::cout << "~Test(" << ::count << ")\n";
--::count;
}

// e.g. Test1 += Test2;
const Test& Test::operator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if(this == &rhs) {
Except ex("operator+= self assignment!");
throw( &ex );
}
n += rhs.n;
return *this;
}

The Except class implementation..

volatile int except = 0;

// Constructor
Except::Except(const char* msg = 0) : data(msg) {
++::except;
std::cout << " Except(" << ::except << ")\n";
}

// Destructor
Except::~Except() {
std::cout << "~Except(" << ::except << ")\n";
--::except;
}

// access data
const char* Except::Get() { return data; }
int main() {
Test t;
try { t += t; }
catch( Except* e ) {
cout << "Throw() caught: " << e->Get() << "\n";
return -1;
}
}

Output:
======
Test(1)
operator+=(Test&)
Except(1)
~Except(1)
Throw() caught: operator+= self assignment!
~Test(1)

But this implies that Except goes out of scope before
the throw is caught. I would have liked to have seen:

Test(1)
operator+=(Test&)
Except(1)
Throw() caught: operator+= self assignment!
~Except(1)
~Test(1)

Anyone comments?

TIA
jam

--
"Capitalism is the extraordinary belief that the
nastiest of men, for the nastiest of reasons, will
somehow work for the benefit of us all."
[John Maynard Keynes]


Jul 22 '05 #1
6 1445
Jackson A. Marshall wrote:
I have a been testing the try/catch exception mechanism
(as implemented by DigitalMars) and found that if I
throw an object its dtor gets called 3 times, if I throw
a reference its dtor gets called twice - and so it seems
practical to instantiate an object and throw its address,
and sure enough, its dtor gets called only once.

This is part of the Test class implementation (contains only
one data member: "int n" ):
[...]
Anyone comments?


Here is a comment: regarding your code and the sequence of d-tors,
you're throwing an address of a local variable. It gets destroyed
before you catch it. When you after catching the value of the
pointer attempt to use it, you get undefined behavour.

And also your 'Test' class doesn't seem to have any copy c-tor
implemented, thus making it impossible for you to track copy
construction (not that you should expect any, but still).

If you want to throw by an address (pointer), you have to use
either a global object (that survives the stack unwinding) or
a dynamic object (use 'new'). Of course, it's better to throw
a temporary object and catch by a const reference.

V
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:F8*******************@newsread1.mlpsca01.us.t o.verio.net...
Jackson A. Marshall wrote:
I have a been testing the try/catch exception mechanism
(as implemented by DigitalMars) and found that if I
throw an object its dtor gets called 3 times, if I throw
a reference its dtor gets called twice - and so it seems
practical to instantiate an object and throw its address,
and sure enough, its dtor gets called only once.

This is part of the Test class implementation (contains only
one data member: "int n" ):
[...]

Anyone comments?


Here is a comment: regarding your code and the sequence of d-tors,
you're throwing an address of a local variable. It gets destroyed
before you catch it. When you after catching the value of the
pointer attempt to use it, you get undefined behavour.

And also your 'Test' class doesn't seem to have any copy c-tor
implemented, thus making it impossible for you to track copy
construction (not that you should expect any, but still).

If you want to throw by an address (pointer), you have to use
either a global object (that survives the stack unwinding) or
a dynamic object (use 'new'). Of course, it's better to throw
a temporary object and catch by a const reference.


Thanks V

I do have a copy-ctor, just didn't show it:

// Copy Constructor
Test::Test(Test& t) : n(t.n) {
++::count;
std::cout << " Test(" << ::count << ") Copy-Ctor\n";
}

Using "new" :

// e.g. Test1 += Test2;
const Test& Test::operator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if(this == &rhs) {
Except* ex = new Except("operator+= self assignment!");
throw( ex );
}
n += rhs.n;
return *this;
}

// copy ctor: pass by value
void test1(Test a) {}

int main() {
Test t;
test1(t); // copy ctor
cout << "\n";

try { t += t; }
catch( Except* e) {
cout << "Throw() caught: " << e->Get() << "\n";
delete e;
return -1;
}
}

Output:
=====
Test(1)
Test(2) Copy-Ctor
~Test(2)

operator+=(Test&)
Except(1)
Throw() caught: operator+= self assignment!
~Except(1)
~Test(1)
===============

What do you mean by (and how do I construct) a "temporary object"?
jam

Jul 22 '05 #3
Jackson A. Marshall wrote:
[...]
What do you mean by (and how do I construct) a "temporary object"?


Try

const Test& Test::operator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if (this == &rhs)
throw(Except("operator+= self assignment!")); // temporary

n += rhs.n;
return *this;
}
...
int main() {
Test t;
try { t += t; }
catch(Except const &e) {
cout << "Caught " << e.Get() << endl;
}
return -1;
}

V
Jul 22 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:Jh******************@newsread1.mlpsca01.us.to .verio.net...
Jackson A. Marshall wrote:
[...]
What do you mean by (and how do I construct) a "temporary object"?
Try

const Test& Test::operator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if (this == &rhs)
throw(Except("operator+= self assignment!")); // temporary

n += rhs.n;
return *this;
}
...
int main() {
Test t;
try { t += t; }
catch(Except const &e) {
cout << "Caught " << e.Get() << endl;
}
return -1;
}


Thanks V

I have implemented the code changes as you suggest.

Test(1)
operator+=(Test&)
Except(1)
~Except(1)
Throw() caught: operator+= self assignment!
~Except(0)
~Test(1)

...but this is incorrect with 2 Except dtors!
jam



Jul 22 '05 #5
Jackson A. Marshall wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:Jh******************@newsread1.mlpsca01.us.to .verio.net...
Jackson A. Marshall wrote:
[...]
What do you mean by (and how do I construct) a "temporary object"?


Try

const Test& Test::operator+=(const Test& rhs) {
std::cout << "operator+=(Test&)\n";
if (this == &rhs)
throw(Except("operator+= self assignment!")); // temporary

n += rhs.n;
return *this;
}
...
int main() {
Test t;
try { t += t; }
catch(Except const &e) {
cout << "Caught " << e.Get() << endl;
}
return -1;
}

Thanks V

I have implemented the code changes as you suggest.

Test(1)
operator+=(Test&)
Except(1)
~Except(1)
Throw() caught: operator+= self assignment!
~Except(0)
~Test(1)

..but this is incorrect with 2 Except dtors!


Are you sure that you got every Except's _constructor_ accounted for?
What about the copy constructor?
Jul 22 '05 #6
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:7L*******************@newsread1.mlpsca01.us.t o.verio.net...

Are you sure that you got every Except's _constructor_ accounted for?
What about the copy constructor?
Many thanks V

I didn't have an Except copy-ctor, but do now :

// Copy Constructor
Except::Except( const Except& ex) : data(ex.data) {
++::except;
std::cout << " Except(" << ::except << ") Copy-Ctor\n";
}

Output:
Test(1)
operator+=(Test&)
Except(1)
Except(2) Copy-Ctor
~Except(2)
Throw() caught: operator+= self assignment!
~Except(1)
~Test(1)

Thanks againjam


Jul 22 '05 #7

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

Similar topics

4
by: Zork | last post by:
Hi, I am trying to stop object creation (in this case ill use a ball as the object) via use of exceptions. In essence, if the ball does not have an owner, I do not want the ball object created....
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
5
by: Hansen | last post by:
Hi! I have a problem with an object not yet being initialized when I try to access it. Hence I would like to wait for the object to be initialized. I have the following code: try {...
11
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the...
3
by: John Rivers | last post by:
try catch blocks create a new scope i don't think that they should for example, to add error handling to this statement: int x = 1; you have to rewrite it as a seperate declaration and...
1
by: William Cruz | last post by:
Can anyone help me with this piece of code. - I keep on getting the error message "The object exporter specified was not found" If i change it to run on my local machine it works fine, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.