C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Should exceptions be thrown by value or reference?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rennie deGraaf
Guest





PostPosted: Mon Feb 28, 2005 12:55 am    Post subject: Should exceptions be thrown by value or reference? Reply with quote



Is it better/more efficient to throw exceptions by value, as in

void func() throw (SomeException)
{
// do some stuff
throw SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException e)
{
// do some stuff
}
}

or by reference, as in

void func() throw (SomeException*)
{
// do some stuff
throw new SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException* e)
{
// do some stuff
delete e;
}
}

? Either method requires allocating memory (on the stack in the first
case, on the heap in the second), so I don't think that either is any
less safe than the other. Obviously, the method of throwing pointers is
prone to memory leaks if programmers are not careful, but is it any more
or less efficient?

Rennie
Back to top
Ioannis Vranos
Guest





PostPosted: Mon Feb 28, 2005 1:26 am    Post subject: Re: Should exceptions be thrown by value or reference? Reply with quote



Rennie deGraaf wrote:
Quote:

Is it better/more efficient to throw exceptions by value, as in

void func() throw (SomeException)
{
// do some stuff
throw SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException e)
{
// do some stuff
}
}

or by reference, as in

void func() throw (SomeException*)
{
// do some stuff
throw new SomeException("error message");
}

void bar()
{
try
{
foo()
}
catch (SomeException* e)
{
// do some stuff
delete e;
}
}


or

void func() throw (SomeException)
{
// do some stuff
throw SomeException("error message");
}

void bar()
{
try
{
func();
}

catch (SomeException &re)
{
// do some stuff
}
}



Quote:
? Either method requires allocating memory (on the stack in the first
case, on the heap in the second), so I don't think that either is any
less safe than the other. Obviously, the method of throwing pointers is
prone to memory leaks if programmers are not careful, but is it any more
or less efficient?


By reference (with references or pointers) is of course more space and
time efficient since no new exception object is created.


In non-GC systems, I would prefer references.



--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Cy Edmunds
Guest





PostPosted: Mon Feb 28, 2005 1:32 am    Post subject: Re: Should exceptions be thrown by value or reference? Reply with quote



"Rennie deGraaf" <ca.ucalgary.cpsc@degraaf> wrote

Quote:
Is it better/more efficient to throw exceptions by value, as in

void func() throw (SomeException)

Read this before you use exception specifications:
http://www.gotw.ca/publications/mill22.htm

Quote:
{
// do some stuff
throw SomeException("error message");
}

Otherwise this is OK, especially if SomeException is ultimately derived from
std::exception.

Quote:

void bar()
{
try
{
foo()
}
catch (SomeException e)
{
// do some stuff
}
}

This is OK but don't expect any sort of polymorphic behavior. If you derive
another class from SomeException it will get bit sliced down to a
SomeException unless you change your catch phrase to

catch (SomeException &e)

Quote:

or by reference, as in

void func() throw (SomeException*)
{
// do some stuff
throw new SomeException("error message");
}

I can't imagine why anyone would do this.

Quote:

void bar()
{
try
{
foo()
}
catch (SomeException* e)
{
// do some stuff
delete e;
}
}

? Either method requires allocating memory (on the stack in the first
case, on the heap in the second), so I don't think that either is any less
safe than the other. Obviously, the method of throwing pointers is prone
to memory leaks if programmers are not careful, but is it any more or less
efficient?

Rennie

I don't understand why everyone is so concerned about efficiency in a
situation (propogation of an error) which one would certainly hope would
constitute an extremely small portion of the run time.

--
Cy
http://home.rochester.rr.com/cyhome/



Back to top
Ioannis Vranos
Guest





PostPosted: Mon Feb 28, 2005 2:25 am    Post subject: Re: Should exceptions be thrown by value or reference? Reply with quote

Cy Edmunds wrote:

Quote:
I don't understand why everyone is so concerned about efficiency in a
situation (propogation of an error) which one would certainly hope would
constitute an extremely small portion of the run time.


Basically where exceptions "by reference" (references or pointers) make
sense are on severely run-time/space constrained systems, when exception
types have no public copy constructors, and in GC systems I guess - in
..NET in addition to the usual unmanaged ISO C++ exception styles that
were mentioned here, managed exceptions are created in the managed heap
and caught as managed pointers:


try
{
throw __gc new SomeException;
}


catch(SomeException *pe)
{
// ...
}



--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.