Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why should throwing an exception by reference work

Reply
Thread Tools

why should throwing an exception by reference work

 
 
puzzlecracker
Guest
Posts: n/a
 
      05-10-2006
It shouldnt, since reference object is destroyed in the stack frame.

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      05-10-2006
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?
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      05-10-2006

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(...){}

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      05-10-2006
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.
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      05-10-2006
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?
 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-10-2006

> 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
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      05-11-2006
"Tomás" <(E-Mail Removed)> wrote in message
news:Jzu8g.9030$(E-Mail Removed)
> "puzzlecracker" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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



 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      05-11-2006

John Carson wrote:
> "Tomás" <(E-Mail Removed)> wrote in message
> news:Jzu8g.9030$(E-Mail Removed)
> > "puzzlecracker" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) 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?

 
Reply With Quote
 
Zara
Guest
Posts: n/a
 
      05-11-2006
On 10 May 2006 18:38:54 -0700, "puzzlecracker" <(E-Mail Removed)>
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
 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      05-11-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
WebRequest.GetResponse() throwing exception (Internal Server error =?Utf-8?B?VGVycnk=?= ASP .Net 4 01-13-2005 10:25 PM
throwing out of memory exception in c++ doesnt work Stephan C++ 22 03-07-2004 05:37 PM
Problem of throwing an exception (System.Net.Sockets.OverlappedAsyncResult::CompletionPortCallback) VincentWong ASP .Net 1 12-29-2003 09:32 AM
Throwing Exception in a composite control KJ ASP .Net 5 07-25-2003 01:33 PM



Advertisments