Should exceptions be thrown by value or reference?

R

Rennie deGraaf

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
 
I

Ioannis Vranos

Rennie said:
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
}
}


? 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.
 
C

Cy Edmunds

Rennie deGraaf said:
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
{
// do some stuff
throw SomeException("error message");
}

Otherwise this is OK, especially if SomeException is ultimately derived from
std::exception.
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)
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.
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.
 
I

Ioannis Vranos

Cy said:
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)
{
// ...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top