 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rennie deGraaf Guest
|
Posted: Mon Feb 28, 2005 12:55 am Post subject: Should exceptions be thrown by value or reference? |
|
|
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
|
Posted: Mon Feb 28, 2005 1:26 am Post subject: Re: Should exceptions be thrown by value or reference? |
|
|
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
|
Posted: Mon Feb 28, 2005 1:32 am Post subject: Re: Should exceptions be thrown by value or reference? |
|
|
"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
|
Posted: Mon Feb 28, 2005 2:25 am Post subject: Re: Should exceptions be thrown by value or reference? |
|
|
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 |
|
 |
|
|
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
|
|