473,378 Members | 1,504 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,378 software developers and data experts.

Value from exception object

consider the following program:

#include <cstdlib>
#include <iostream>

using namespace std;

class Test
{
public:
Test();
Test(const Test &rhs);
inline int value() const;

private:
int x;
};

Test::Test()
{
cout << "From Test default ctor" << endl;
x = 100;
}

inline int Test::value() const
{
return x;
}

Test::Test(const Test &rhs)
{
cout << "From Test copy ctor" << endl;
}

void fn1()
{
Test obj;
throw obj;
}

int main()
{
try
{
fn1();
}
catch (const Test &e)
{
cout << "Exception caught. Value = " << e.value() <<
endl;
}

return EXIT_SUCCESS;
}

I compiled this program x.cpp as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

The following output is produced:

From Test default ctor
From Test copy ctor
Exception caught. Value = 0

Here why is the value is 0 and not 100 ?

However if I have the function fn1() as
void fn1()
{
throw Test();
}

the following output is produced.

From Test default ctor
Exception caught. Value = 100

Now the value is printed as 100 and also the copy ctor message is not
present.
Why is this difference ?

Kindly clarify.
Thanks
V.Subramanian
Jul 17 '08 #1
1 1371
su**************@yahoo.com, India wrote:
consider the following program:
<snip>
>
I compiled this program x.cpp as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

The following output is produced:

From Test default ctor
From Test copy ctor
Exception caught. Value = 0

Here why is the value is 0 and not 100 ?
Because your copy construct or did not do its job.
Test::Test(const Test &rhs)
{
cout << "From Test copy ctor" << endl;
}
No copy of the value.
However if I have the function fn1() as
void fn1()
{
throw Test();
}

the following output is produced.

From Test default ctor
Exception caught. Value = 100

Now the value is printed as 100 and also the copy ctor message is not
present.
Why is this difference ?
The copy is optimised away in this case. Remember throw is by value, in
the first instance, obj was copied when it was thrown.

--
Ian Collins.
Jul 17 '08 #2

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

Similar topics

21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
6
by: David N | last post by:
Hi All, What is a best way to handle an undefined value object class that returned from a function. I have a function that call the ADO.NET ExecuteScalar() function and returns the object to...
0
by: Emmanuel | last post by:
Hi, I 've managed to run successfully the example provided by the Microsoft Q302901 - "How To Build an Office COM Add-in by Using Visual C# .NET" article...
5
by: lds | last post by:
I am getting the following error: The "SendUsing" configuration value is invalid. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
0
by: Siegfried Heintze | last post by:
This program works fine on my desktop when I grant full control of the MSAccess database to everyone. However, when I put it on my hosting service with no impersonation, I now get this error (see...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
2
by: Jim McGivney | last post by:
In asp 2.0 I am trying to insert a row using a detailsview control connected to an accessDataSource. I get the error message below. I am having trouble identifing which data field is causing the...
0
by: flyingchen | last post by:
using System; using System.Windows.Forms; using System.Threading; using System.ComponentModel; namespace ProgressControl.Core { public delegate object Execute(params object args); public...
0
by: AxleWacl | last post by:
Hi, The below error is what I am receiving. The code im using is below the error, for the life of me, I can not see where any parameter is missing..... Server Error in '/FleetcubeNews'...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.