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

why should throwing an exception by reference work

It shouldnt, since reference object is destroyed in the stack frame.

May 10 '06 #1
10 1678

puzzlecracker wrote:
It shouldnt, since reference object is destroyed in the stack frame.

in this example

void bar(){
MyExcepton ex(); //MyException inherits from Excetpion
Exception &exRef=ex;
throw exRef;

}

void foo(){

try{

bar();
}catch( Exception &ex){

}catch(...){}

May 10 '06 #2
void bar(){
MyExcepton ex(); //MyException inherits from Excetpion
That's a very pretty function declaration you have there. It's exactly the
same as writing:

MyException ex(void);

ReturnType FunctionName(ParameterList);

What you want is:

My Exception ex;
Exception &exRef=ex;
throw exRef;

}

void foo(){

try{

bar();
}catch( Exception &ex){

You have an invalid reference here, because the object to which it refers
has gone out of scope (i.e. has been destroyed).

You could throw a reference if it's valid. For example:

int global_var = 2;

int main()
{
try
{

throw global_var;
}
catch (int &i)
{
i = 7;
}
}
-Tomás
May 10 '06 #3
On Wed, 10 May 2006 16:11:45 -0700, puzzlecracker wrote:
It shouldnt, since reference object is destroyed in the stack frame.


Throwing an exception by reference? Or catching it?
May 10 '06 #4
On Wed, 10 May 2006 16:15:04 -0700, puzzlecracker wrote:
puzzlecracker wrote:
It shouldnt, since reference object is destroyed in the stack frame.

in this example

void bar(){
MyExcepton ex(); //MyException inherits from Excetpion
Exception &exRef=ex;
throw exRef;

}

void foo(){

try{

bar();
}catch( Exception &ex){

}catch(...){}

Ah... but when the exception is thrown... a copy is taken and put
/somewhere/ (implementation-defined, I believe). So when you catch,
you're getting a reference to that copied object.
May 10 '06 #5
On Wed, 10 May 2006 23:17:08 +0000, Andre Kostur wrote:
On Wed, 10 May 2006 16:15:04 -0700, puzzlecracker wrote:
puzzlecracker wrote:
It shouldnt, since reference object is destroyed in the stack frame.

in this example

void bar(){
MyExcepton ex(); //MyException inherits from Excetpion


BTW, doesn't this declare a function named "ex", taking no parameters, and
returning and object of type MyException, by value?
May 10 '06 #6
"Tomás" <NU**@NULL.NULL> wrote in message
news:Jz******************@news.indigo.ie
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com
void bar(){
MyExcepton ex(); //MyException inherits from Excetpion


That's a very pretty function declaration you have there. It's
exactly the same as writing:

MyException ex(void);

ReturnType FunctionName(ParameterList);

What you want is:

My Exception ex;
Exception &exRef=ex;
throw exRef;

}

void foo(){

try{

bar();
}catch( Exception &ex){

You have an invalid reference here, because the object to which it
refers has gone out of scope (i.e. has been destroyed).


I don't believe that is correct. Consider:

#include <iostream>
using namespace std;

struct MyException
{
int a;
};

void bar()
{
MyException me = {4};
MyException &meRef = me;
throw meRef;
}

int main()
{
try
{
bar();
}
catch( MyException &me)
{
cout << "integer is " << me.a << '\n';
}
return 0;
}

This works as it should on VC++ 8 and compiles without warnings on both VC++
8 and Comeau online.

I believe the relevant section of the standard is 15.1/3:

"A throw-expression initializes a temporary object, called the exception
object, the type of which is determined by removing any top-level
cv-qualifiers from the static type of the operand of throw and adjusting the
type from "array of T" or "function returning T" to "pointer to T" or
"pointer to function returning T", respectively. [Note: the temporary object
created for a throw-expression that is a string literal is never of type
char* or wchar_t*; that is, the special conversions for string literals from
the types "array of const char" and "array of const wchar_t" to the types
"pointer to char" and "pointer to wchar_t", respectively (4.2), are never
applied to a throw-expression. ] The temporary is used to initialize the
variable named in the matching handler (15.3)."

On my reading, this means that the throw expression creates its own
temporary, so it is not dependent on the one declared inside bar() in the
code above (15.1/5 says that the creation of the temporary may be eliminated
if it doesn't change the meaning of the program).

It is of some interest to note that it apparently not necessary to make the
argument to catch() const in spite of the fact that it is being initialized
by a temporary.

--
John Carson

May 11 '06 #7

John Carson wrote:
"Tomás" <NU**@NULL.NULL> wrote in message
news:Jz******************@news.indigo.ie
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com
void bar(){
MyExcepton ex(); //MyException inherits from Excetpion


That's a very pretty function declaration you have there. It's
exactly the same as writing:

MyException ex(void);

ReturnType FunctionName(ParameterList);

What you want is:

My Exception ex;
Exception &exRef=ex;
throw exRef;

}

void foo(){

try{

bar();
}catch( Exception &ex){

You have an invalid reference here, because the object to which it
refers has gone out of scope (i.e. has been destroyed).


I don't believe that is correct. Consider:

#include <iostream>
using namespace std;

struct MyException
{
int a;
};

void bar()
{
MyException me = {4};
MyException &meRef = me;
throw meRef;
}

int main()
{
try
{
bar();
}
catch( MyException &me)
{
cout << "integer is " << me.a << '\n';
}
return 0;
}

This works as it should on VC++ 8 and compiles without warnings on both VC++
8 and Comeau online.

I believe the relevant section of the standard is 15.1/3:

"A throw-expression initializes a temporary object, called the exception
object, the type of which is determined by removing any top-level
cv-qualifiers from the static type of the operand of throw and adjusting the
type from "array of T" or "function returning T" to "pointer to T" or
"pointer to function returning T", respectively. [Note: the temporary object
created for a throw-expression that is a string literal is never of type
char* or wchar_t*; that is, the special conversions for string literals from
the types "array of const char" and "array of const wchar_t" to the types
"pointer to char" and "pointer to wchar_t", respectively (4.2), are never
applied to a throw-expression. ] The temporary is used to initialize the
variable named in the matching handler (15.3)."

On my reading, this means that the throw expression creates its own
temporary, so it is not dependent on the one declared inside bar() in the
code above (15.1/5 says that the creation of the temporary may be eliminated
if it doesn't change the meaning of the program).

It is of some interest to note that it apparently not necessary to make the
argument to catch() const in spite of the fact that it is being initialized
by a temporary.

--
John Carson


John - that does it, i guess.

Reading Java Language specification
(http://java.sun.com/docs/books/jls/t...tml/j3TOC.html) is
much easier than c++ standard.... is that why c++ is dying?:)

May 11 '06 #8
On 10 May 2006 18:38:54 -0700, "puzzlecracker" <ir*********@gmail.com>
wrote:

<....>
Reading Java Language specification
(http://java.sun.com/docs/books/jls/t...tml/j3TOC.html) is
much easier than c++ standard.... is that why c++ is dying?:)

Ha, nice troll trap.

Starting a thread, and the deiverging to some hot topic.

Eat a carrot and feel fed, troll.

Zara
May 11 '06 #9

John Carson wrote:
It is of some interest to note that it apparently not necessary to make the
argument to catch() const in spite of the fact that it is being initialized
by a temporary.


probably because there is no chance of an implicit conversion in a
catch. It simply won' catch.

void foo()
{
throw ("text");
}

void bar()
{
try
{
foo();
}
catch ( std::string & s ) // won't catch the exception
{
s.push_back( 'X' );
}
}

And if you put const std::string & instead it still won't catch it.

May 11 '06 #10

Earl Purple wrote:
John Carson wrote:
It is of some interest to note that it apparently not necessary to make the
argument to catch() const in spite of the fact that it is being initialized
by a temporary.


probably because there is no chance of an implicit conversion in a
catch. It simply won' catch.

void foo()
{
throw ("text");
}

void bar()
{
try
{
foo();
}
catch ( std::string & s ) // won't catch the exception
{
s.push_back( 'X' );
}
}

And if you put const std::string & instead it still won't catch it.


why not?

is it because, you're throwing char *???

May 11 '06 #11

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

Similar topics

21
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
13
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my...
15
by: Sek | last post by:
Gurus, I am wondering whether it is right to throw an exception from a Property of an object. To get into it further, is it okay to throw exception during 'get' operation? I was searching...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
1
by: usenet | last post by:
I wrote some sample code (see below) for nested exception throwing i.e. my catch blocks are throwing exceptions of their own (for simplicity I used standard exceptions). I am getting some...
3
balabaster
by: balabaster | last post by:
Hey all, this might seem somewhat remedial but I'm unable to find any documentation on it - it pertains to throwing exceptions within a DLL. I've written a DLL that contains a bunch of classes....
2
by: Rahul | last post by:
Hi everyone, I have the following code and it doesn't give any compilation error, class A { public : A() { } void show()
9
by: thagor2008 | last post by:
Is the behaviour of throwing exceptions in a unix signal handler defined? eg: void sighandler(int sig) { ... do something throw myobj; }
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
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...
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
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
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,...
0
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...
0
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
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...

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.